注册 登录
编程论坛 C++教室

求助 这个有关运算符重载程序在编译器不能通过

a654548060 发布于 2010-09-05 09:57, 756 次点击
#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)
4 回复
#2
luzhiwei5122010-09-05 12:21
重载+没有在类中声明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;
}


#3
pangding2010-09-05 12:56
就是这个问题,编译器给的第一个错误说的很明确:error C2039: '+' : is not a member of 'Complex'
#4
a6545480602010-09-05 21:47
谢谢
#5
dongfanliang2010-09-10 11:31
#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;
}
1