linux g++ Line 3: u :(1,2) Linux g++ code running段错误

BMCRNET 2008-08-30 05:01:42

#include <iostream>

using namespace std;

class op_ov_class
{
friend ostream &operator<<(ostream&,const op_ov_class&);
friend istream &operator>>(istream&,const op_ov_class&);
public:
op_ov_class operator+(const op_ov_class&) const;
op_ov_class operator-(const op_ov_class&) const;
op_ov_class(int i=0,int j=0);
private:
int a;
int b;
};
ostream& operator<<(ostream &os,const op_ov_class& right)
{
os<<"("<<right.a<<","<<right.b<<")";
return os;
}

istream& operator>>(istream &is,const op_ov_class& right)
{
is>>right.a>>right.b;
return is;
}
op_ov_class op_ov_class::operator+(const op_ov_class &right) const
{
op_ov_class tmp;
tmp.a = a+right.a;
tmp.b = b+right.b;
return tmp;
}
op_ov_class op_ov_class::operator-(const op_ov_class &right) const
{
op_ov_class tmp;
tmp.a = a-right.a;
tmp.b = b-right.b;
return tmp;
}
op_ov_class::op_ov_class(int i,int j)
{
a = i;
b = j;
}
int main()
{

op_ov_class u(1,2);
op_ov_class v;
cout<<"Line 3: u :"<<u<<endl;
cout<<"Line 4: Entry two integers: ";
cin>>v;
cout<<endl;
cout<<"Line 7: v : "<<v<<endl;
cout<<"Line 8: u+v :"<<u+v<<endl;
cout<<"Line 9: u-v :"<<u-v<<endl;
cout<<"hello world"<<endl;
return 0;
}

running the program .flow the error
Line 3: u :(1,2)
段错误



...全文
64 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
BMCRNET 2008-08-31
  • 打赏
  • 举报
回复
呵呵
搞定!C++真是不好学啊呵呵!比C复杂多了!
脚跟着地 2008-08-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lifanxi 的回复:]
istream &operator>>(istream&,const op_ov_class&);
这个成员函数定义不正确。
cin >> 是会改变op_ov_class的对象的值的,所以传入的不能是const的引用,改成
istream &operator>>(istream&,op_ov_class&);
就可以了。
[/Quote]
正解
K行天下 2008-08-30
  • 打赏
  • 举报
回复
const不能滥用,要注意哪些是需要改写的
lifanxi 2008-08-30
  • 打赏
  • 举报
回复
istream &operator>>(istream&,const op_ov_class&);
这个成员函数定义不正确。
cin >> 是会改变op_ov_class的对象的值的,所以传入的不能是const的引用,改成
istream &operator>>(istream&,op_ov_class&);
就可以了。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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