一个函数返回值为vector类型的问题

hxwangcong 2009-07-03 04:40:50
我的代码这样写:

#include "iostream"
#include "vector"
using namespace std;

vector<int> plus(vector<int> &v1,vector<int> &v2)
{
int lenth=v1.size()>v2.size()?v1.size():v2.size();
vector<int> v(lenth+1);
for(int i=0;i<lenth;i++)
{
v[i]=v1[i]+v2[i];
if(v[i]>=10)
{
v[i+1]++;
v[i]-=10;
}
}
if(v[lenth]==0)
{
vector<int>::iterator it=v.end()-1;
v.erase(it);
}
return v;
}

int main()
{
vector<int> v1;
vector<int> v2;
v1.push_back(3);
v1.push_back(5);
v1.push_back(8);
v2.push_back(9);
v2.push_back(9);
v2.push_back(9);
vector<int> v3;
v3=plus(v1,v2);
for(int i=0;i<v3.size();i++)
{
cout<<v3[i];
}
//system("pause");
return 0;
}



VC++里编译通过,但DEV-CPP里编不过去,错误信息:
In function `int main()':
1.cpp non-lvalue in unary `&'
no match for call to `(std::plus<_Tp>) (std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&)'
note d:\DEV-CPP\include\c++\3.4.2\bits\stl_function.h:138 candidates are: _Tp std::plus<_Tp>::operator()(const _Tp&, const _Tp&) const
...全文
254 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengzhixi 2009-07-03
  • 打赏
  • 举报
回复
名字冲突了 !
Walf_ghoul 2009-07-03
  • 打赏
  • 举报
回复
:: === 简单, Debug ===
\main.cpp:54: error: non-lvalue in unary `&'
\main.cpp:54: error: no match for call to `(std::plus<_Tp>) (std::vector<int, std::allocator<int> >&, std::vector<int, std::allocator<int> >&)'
C:\My software\CodeBlocks\CodeBlocks\bin\..\lib\gcc\mingw32\3.4.4\..\..\..\..\include\c++\3.4.4\bits\stl_function.h:138: note: candidates are: _Tp std::plus<_Tp>::operator()(const _Tp&, const _Tp&) const\main.cpp:55: warning: comparison between signed and unsigned integer expressions
:: === Build finished: 3 errors, 1 warnings ===
yshuise 2009-07-03
  • 打赏
  • 举报
回复
这个plus同标准的std::plus发生冲突了,改一个名字。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧