使用&#34; object&lt; <ostream“syntax =”“for =”“a =”“template =”“argument =”“而不是=”“比=”“”object.operator <<< type =“”> (ostream的)&#34;

时间:2017-06-10 09:50:27

标签: c++ oop c++11 operator-overloading

我知道&lt;&lt;运算符也可以作为方法函数重载。当我使用模板参数时,问题出现了。

我意识到我需要指定类,因此我写了&lt; A&gt;在运营商之前&lt;&lt; 。

我应该在哪里写&lt; A&gt;如果我想使用该对象&lt; &LT; ostream语法?

#include <iostream>
#include <malloc.h>

using namespace std;



class A
{
private:
    int x;

public :

A(int i = 0)
{
    x = i;

}
A operator + (A temp)
{
    return x + temp.x;
}

template <class T>
ostream & operator << (ostream &);
};

template <class T>
ostream& A::operator<<(ostream &o)
{
o<<x;
return o;

}

int main()
{
A a(33),b(-21);
(a+b).operator<<<A>(cout);
//a+b<<cout; ????
}

0 个答案:

没有答案