#include<iostream>
using namespace std;
template <typename T>
class CComplex {
private:
T r,i;
public:
CComplex(T r=0,T i=0) {
this->r = r;
this->i = i;
cout << "Constructor:(" << this->r <<", "<< this->i << ")" << endl;
}
CComplex(const CComplex& c) {
this->r = c.r;
this->i = c.i;
cout << "Copy-on:(" << this->r << ", " << this->i << ")" << endl;
}
~CComplex() {
cout << "Destructor:(" << this->r << ", " << this->i << ")" << endl;
}
CComplex operator+(CComplex& c);
friend ostream& operator<<(ostream& os, CComplex& c);
};
template <class T>
CComplex<T> CComplex<T>::operator+(CComplex& c) {
CComplex temp;
temp.r = this->r + c.r;
temp.i = this->i + c.i;
return temp;
}
template <class T>
ostream& operator<<(ostream& os,const CComplex<T>& c) {
os << "(" << c.r << ", " << c.i << ")" ;
return os;
}
int main(int argc, const char* argv[]) {
float r1, i1, r2, i2;
cin >> r1 >> i1 >> r2 >> i2;
CComplex<float> c1(r1, i1), c2(r2, i2), c3;
c3 = c1 + c2;
cout << c1 << endl;
cout << c2 << endl;
cout << c3 << endl;
return 0;
}
#include<iostream>
using namespace std;
template <typename T>
class CComplex {
private:
T r,i;
public:
CComplex(T r=0,T i=0) {
this->r = r;
this->i = i;
cout << "Constructor:(" << this->r <<", "<< this->i << ")" << endl;
}
CComplex(const CComplex& c) {
this->r = c.r;
this->i = c.i;
cout << "Copy-on:(" << this->r << ", " << this->i << ")" << endl;
}
~CComplex() {
cout << "Destructor:(" << this->r << ", " << this->i << ")" << endl;
}
CComplex operator+(CComplex& c);
friend ostream& operator <<<> (ostream& os, const CComplex<T>& c);
};
template <typename T>
ostream& operator <<<> (ostream& os, const CComplex<T>& c) {
os << "(" << c.r << ", " << c.i << ")";
return os;
}
template <typename T>
CComplex<T> CComplex<T>::operator+(CComplex<T>& c) {
CComplex temp;
temp.r = this->r + c.r;
temp.i = this->i + c.i;
return temp;
}
int main() {
float r1, i1, r2, i2;
cin >> r1 >> i1 >> r2 >> i2;
CComplex<float> c1(r1, i1), c2(r2, i2), c3;
c3 = c1 + c2;
cout << c1 << endl;
cout << c2 << endl;
cout << c3 << endl;
return 0;
}
主要问题是你的友元类申明时,没有加template <typename T>,下面这样也可以
#include<iostream>
using namespace std;
template <typename T>
class CComplex {
private:
T r,i;
public:
CComplex(T r=0,T i=0) {
this->r = r;
this->i = i;
cout << "Constructor:(" << this->r <<", "<< this->i << ")" << endl;
}
CComplex(const CComplex& c) {
this->r = c.r;
this->i = c.i;
cout << "Copy-on:(" << this->r << ", " << this->i << ")" << endl;
}
~CComplex() {
cout << "Destructor:(" << this->r << ", " << this->i << ")" << endl;
}
CComplex operator+(CComplex& c);
template <typename T>
friend ostream& operator << (ostream& os, const CComplex<T>& c);
};
template <typename T>
ostream& operator << (ostream& os, const CComplex<T>& c) {
os << "(" << c.r << ", " << c.i << ")";
return os;
}
template <typename T>
CComplex<T> CComplex<T>::operator+(CComplex<T>& c) {
CComplex temp;
temp.r = this->r + c.r;
temp.i = this->i + c.i;
return temp;
}
int main() {
float r1, i1, r2, i2;
cin >> r1 >> i1 >> r2 >> i2;
CComplex<float> c1(r1, i1), c2(r2, i2), c3;
c3 = c1 + c2;
cout << c1 << endl;
cout << c2 << endl;
cout << c3 << endl;
return 0;
}
参考如下方法解决:
https://blog.csdn.net/xiamentingtao/article/details/44858363
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632