| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 756 人关注过本帖
标题:求助 这个有关运算符重载程序在编译器不能通过
只看楼主 加入收藏
a654548060
Rank: 2
等 级:论坛游民
帖 子:18
专家分:10
注 册:2010-3-30
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:4 
求助 这个有关运算符重载程序在编译器不能通过
#include <iostream>
using namespace std;
class Complex
{
public:
    Complex(int r=0,int i=0);
    Complex add(Complex c);
    void display();
private:
    int real,imag;
};
Complex ::Complex(int r,int i)
{
   real=r;imag=i;
}
Complex Complex::operator+(Complex c)
{
  Complex temp;
  temp.real=real + c.real;
  temp.imag=imag + c.imag;
  return temp;
}
void Complex::display()
{  
      char* str;
     str=(imag<0)?"":"+";
      cout<<real<<str<<imag<<"i"<<endl;
}
int main()
{
  Complex c1(2,3),c2(4,5);
  Complex c;
  c=c1+c2;     
  cout<<"c1+c2=";
  c.display();
  return 0;
}

pp1.cpp
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(16) : error C2039: '+' : is not a member of 'Complex'
        C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(4) : see declaration of 'Complex'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(19) : error C2248: 'real' : cannot access private member declared in class 'Complex'
        C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(10) : see declaration of 'real'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(19) : error C2065: 'real' : undeclared identifier
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(19) : error C2248: 'real' : cannot access private member declared in class 'Complex'
        C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(10) : see declaration of 'real'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(20) : error C2248: 'imag' : cannot access private member declared in class 'Complex'
        C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(10) : see declaration of 'imag'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(20) : error C2065: 'imag' : undeclared identifier
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(20) : error C2248: 'imag' : cannot access private member declared in class 'Complex'
        C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(10) : see declaration of 'imag'
C:\Documents and Settings\longtingwei\桌面\c++编译\Cpp1.cpp(33) : error C2676: binary '+' : 'class Complex' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.

Cpp1.obj - 8 error(s), 0 warning(s)
搜索更多相关主题的帖子: 运算符 编译 重载 
2010-09-05 09:57
luzhiwei512
Rank: 2
等 级:论坛游民
帖 子:4
专家分:21
注 册:2007-8-9
收藏
得分:9 
重载+没有在类中声明Complex operator+(Complex);
#include <iostream>
using namespace std;
class Complex
{
public:
    Complex(int r=0,int i=0);
    Complex add(Complex c);
    void display();
    Complex operator+(Complex);
    int real,imag;
};
Complex ::Complex(int r,int i)
{
   real=r;imag=i;
}
Complex Complex::operator+(Complex c)
{
  Complex temp;
  temp.real=real + c.real;
  temp.imag=imag + c.imag;
  return temp;
}
void Complex::display()
{
      const char *str;
      str=(imag<0)?"":"+";
      cout<<real<<str<<imag<<"i"<<endl;
}
int main()
{
  Complex c1(2,3),c2(4,5);
  Complex c;
  c=c1+c2;
  cout<<"c1+c2=";
  c.display();
  return 0;
}


2010-09-05 12:21
pangding
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:北京
等 级:贵宾
威 望:94
帖 子:6784
专家分:16751
注 册:2008-12-20
收藏
得分:1 
就是这个问题,编译器给的第一个错误说的很明确:error C2039: '+' : is not a member of 'Complex'
2010-09-05 12:56
a654548060
Rank: 2
等 级:论坛游民
帖 子:18
专家分:10
注 册:2010-3-30
收藏
得分:0 
谢谢
2010-09-05 21:47
dongfanliang
Rank: 1
等 级:新手上路
帖 子:11
专家分:7
注 册:2010-9-10
收藏
得分:0 
#include <iostream>
using namespace std;
class Complex
{
public:
    Complex(int r=0,int i=0);
    Complex add(Complex c);
    void display();
    Complex operator+(Complex c);
private:
    int real,imag;
};
Complex ::Complex(int r,int i)
{
   real=r;imag=i;
}
Complex Complex::operator+(Complex c)
{
  Complex temp;
  temp.real=real + c.real;
  temp.imag=imag + c.imag;
  return temp;
}
void Complex::display()
{  
      char* str;
     str=(imag<0)?"":"+";
      cout<<real<<str<<imag<<"i"<<endl;
}
int main()
{
  Complex c1(2,3),c2(4,5);
  Complex c;
  c=c1+c2;     
  cout<<"c1+c2=";
  c.display();
  return 0;
}
2010-09-10 11:31
快速回复:求助 这个有关运算符重载程序在编译器不能通过
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039923 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved