#include
using namespace std;
template
class A{
friend A operator- (A& m, A&n){
A t(m.a - n.a, m.b - n.b);
return t;
}
friend ostream & operator<< (ostream &out, A &t);
public:
A(){
}
A (T a, T b);
A operator + (A &m);
void show();
private:
T a;
T b;
};
template
ostream & operator<< (ostream &out, A &t){
out << t.a << " + " << t.b << "i";
return out;
}
template
A::A (T a, T b){
this->a = a;
this->b = b;
cout << "init" << endl;
}
template
void A::show(){
cout << "a:" << a << "b:" << b << endl;
}
template
A A::operator+ (A &m){
A t(a + m.a, b + m.b);
return t;
}
int main(){
A t(1,2);
// cout << t << endl;
// A t2(3,4);
// A t3 = t + t2;
// cout << t3 << endl;
// t3 = t - t2;
// cout << t3 << endl;
return 0;
}
g++编译
t2.cpp: In instantiation of ‘class A’:
t2.cpp:48:10: required from here
t2.cpp:10:19: error: template-id ‘operator<< ’ for ‘std::ostream& operator<<(std::ostream&, A&)’ does not match any template declaration
friend ostream & operator<< (ostream &out, A &t);
^
init