模板类的友元重载

模板类的友元重载,注意


#include 

using namespace std;

template 
class Test;                        // 模板类前置声明

template

ostream& operator << (ostream& out, const Test& obj);      // 模板类友元前置声明

template

class Test
{

public:

	friend ostream& operator << <> (ostream& out, const Test& obj);       // 模板类友元定义,注意格式"<>"

	Test(T n=0):num(n){}

	Test(const Test& copy)
	{

		num = copy.num;

	}

private:

	T num;

};

template

ostream& operator << (ostream& out, const Test& obj)
{

	out< t(2);

	cout<

using namespace std;

template

class Test

{

public:

    template                   // 注意S
    friend ostream& operator << (ostream& out, const Test& obj);


    Test(T n=0):num(n) {}



    Test(const Test& copy)

    {

        num = copy.num;

    }



private:

    T num;

};
// 注意都行

template


ostream& operator << (ostream& out, const Test& obj)
{


        out< t(2);


    cout<

你可能感兴趣的:(C++)