2 k876537 k876537 于 2016.04.30 10:57 提问

关于C++的友元函数实现在类的外部问题?
 #include<iostream>

using namespace std;

template<typename T>
class Complex
{
    friend ostream & operator<< <T> (ostream &out,Complex &tmp);
private:
    T a;
    T b;
public:
    Complex(T a, T b);
    void printR();
    Complex operator+(Complex &c);
};
int main()
{
    Complex<int> a(10,10);
    Complex<int> b(10,20);
    Complex<int> c = a+b;
    c.printR();

    return 0;
}
template<typename T>
ostream & operator<<(ostream &out,Complex<T> &tmp)
{
    out<< "a = " << tmp.a <<" b = " << tmp.b << endl;
    return out;
}
template<typename T>
Complex<T>::Complex(T a, T b)  //Complex  类的构造函数。
{
    this-> a = a;
    this-> b = b;
}
template<typename T>
void Complex<T>::printR()       //Complex 类的普通函数.
{
    cout << "a = " << a <<" b = " << b << endl;
}
template<typename T>
Complex<T> Complex<T>::operator+(Complex<T> &c)
{
    Complex<T> tmp(c.a+a,c.b+b);
    return tmp;
}

同样的程序在VS里面可以,但codeblocks里面却不行,应该怎么改呢?

1个回答

devmiao
devmiao   Ds   Rxr 2016.05.01 13:24
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!