输入运算符的重载问题

beITgirl 2011-10-09 06:04:19
这是我写的输入操作符的重载函数,想实现输入一个vector的功能,不知道哪错了,vs报错“error C2601: “operator >>”: 本地函数定义是非法的 ”不知道咋回事啊,求大神指导,在线等啊。

istream &operator >>(istream& in ,vector<int> v) {
int temp;
in>>temp;
v.push_back(temp);
return in;
}
...全文
199 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nand库路泽 2011-10-21
  • 打赏
  • 举报
回复
VS2010也是正常的!!
beITgirl 2011-10-09
  • 打赏
  • 举报
回复
啊 问题终于解决了,我把重载函数放在了main函数里面,所以一直报错,应该放main函数外面
beITgirl 2011-10-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 matrixcl 的回复:]
头文件包含对了吗?


C/C++ code

#include <iostream>
#include <vector>
using namespace std;



头文件是这样写的,它报错的地方我仔仔细细检查了,始终没发现哪里不对劲,还是编译不通过
beITgirl 2011-10-09
  • 打赏
  • 举报
回复
本地函数定义是非法的?到底啥意思啊,不懂
matrixcl 2011-10-09
  • 打赏
  • 举报
回复
头文件包含对了吗?

#include <iostream>
#include <vector>
using namespace std;


[Quote=引用 5 楼 hnuqinhuan 的回复:]

C/C++ code

#include <iostream>
#include <vector>

using namespace std;

istream &operator >>(istream& in ,vector<int>& v)
{
int temp;
while( in >> temp )
v.push_back(t……
[/Quote]
楼主的代码编译是没有问题的,但是有逻辑问题,这里应该传引用。 但是这里传引用不是“vc良好的格式”要求,而是逻辑要求。
beITgirl 2011-10-09
  • 打赏
  • 举报
回复
恩 谢谢 记住了,但还是那个问题,不知道咋回事,继续泪奔~~~~~
無_1024 2011-10-09
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>

using namespace std;

istream &operator >>(istream& in ,vector<int>& v)
{
int temp;
while( in >> temp )
v.push_back(temp);
return in;
}
ostream &operator <<(ostream &out,const vector<int>& v)
{
for( int i = 0; i < v.size(); ++i )
{
out << v.at(i) << ends;
}
out << endl;
return out;
}
int main()
{
vector<int> vv;
cin >> vv;
cout << vv;
return 0;
}
//vc良好的格式是用按引用传参数

beITgirl 2011-10-09
  • 打赏
  • 举报
回复
不行啊,这是我用DevC++编译的结果,11 C:\Users\Administrator\Desktop\Untitled1.cpp a function-definition is not allowed here before '{' token 11 C:\Users\Administrator\Desktop\Untitled1.cpp expected `,' or `;' before '{' token
我仔细检查了,括号没有错啊,泪奔了~~~~~
peeky01 2011-10-09
  • 打赏
  • 举报
回复
istream &operator >>(istream& in ,vector<int>& v)
無_1024 2011-10-09
  • 打赏
  • 举报
回复
VS2010也可以运行
無_1024 2011-10-09
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>

using namespace std;

istream &operator >>(istream& in ,vector<int> v)
{
int temp;
in>>temp;
v.push_back(temp);
return in;
}

int main()
{
vector<int> vv;
cin >> vv;
return 0;
}
//vc6.0可以运行

33,311

社区成员

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

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