主题:运算符重载和友元函数的小问题
#include <iostream>
using namespace std;
class complex
{
int real;
int imag;
public:
complex(int a,int b){real=a;imag=b}
complex operator+(complex &);
complex operator+(int&);
friend complex operator +(int &,complex &);//问题在这里,到底是什么问题呢
void print(){cout<<a<<"."<<b<<"i"<<endl;}
};
complex complex::operator +(complex &c)
{
return complex(real+c.real;imag+c.imag);
}
complex complex::operator +(int &c)
{
return complex(real+c;imag+0);
}
complex operator +(int &b,complex &c)
{
return complex(b+c.real;0+c.imag);
}
int main()
{
complex a(1,2),b(2,3),c;
c=a+b;
c.print();
int i=5;
c=5+a;
c.print();
c=a+5;
c.print();
}
Compiling...
COMPLEX.CPP
D:\学习资料\c++\运算符重载\代码练习\COMPLEX.CPP(12) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
执行 cl.exe 时出错.
COMPLEX.OBJ - 1 error(s), 0 warning(s)
请执教谢谢谢谢~~~~~鞠躬~~~~
using namespace std;
class complex
{
int real;
int imag;
public:
complex(int a,int b){real=a;imag=b}
complex operator+(complex &);
complex operator+(int&);
friend complex operator +(int &,complex &);//问题在这里,到底是什么问题呢
void print(){cout<<a<<"."<<b<<"i"<<endl;}
};
complex complex::operator +(complex &c)
{
return complex(real+c.real;imag+c.imag);
}
complex complex::operator +(int &c)
{
return complex(real+c;imag+0);
}
complex operator +(int &b,complex &c)
{
return complex(b+c.real;0+c.imag);
}
int main()
{
complex a(1,2),b(2,3),c;
c=a+b;
c.print();
int i=5;
c=5+a;
c.print();
c=a+5;
c.print();
}
Compiling...
COMPLEX.CPP
D:\学习资料\c++\运算符重载\代码练习\COMPLEX.CPP(12) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
执行 cl.exe 时出错.
COMPLEX.OBJ - 1 error(s), 0 warning(s)
请执教谢谢谢谢~~~~~鞠躬~~~~