重载朋友运算符<<为模板类 [英] Overloading friend operator << for template class

查看:114
本文介绍了重载朋友运算符<<为模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重载操作符<<作为模板类对的朋友,但我不断得到编译器警告说

 朋友声明std :: ostream& <<<<<<(ostream& out,Pair< T,U>& v)声明一个非模板函数



代码:

  friend ostream& operator<<(ostream& pair;< T,U>& 

它会发出第二个警告,表示

 如果这不是你想要的,请确保函数模板已经声明,并添加<>之后的函数名称为

这里是函数定义

 模板< class T,class U> 
ostream&运算符<<<(ostream& out,Pair< T,U>& v)
{
out& v.val1<< < v.val2;
}

这里是整个类。

 模板< class T,class U> 
class Pair {
public:
Pair(T v1,U v2):val1(v1),val2(v2){}
〜Pair(){}
对& operator =(const Pair&);
friend ostream& operator<<(ostream& pair;< T,U>&

private:
T val1;
U val2;
};

我不确定从建议警告中得到什么,除此之外,朋友声明中的某处。有人知道正确的语法吗?感谢。

解决方案

您声明操作符<作为返回一个ostream&,但在该方法中根本没有返回语句。应为:

 模板< class T,class U& 
ostream&运算符<<<(ostream& out,Pair< T,U>& v)
{
return out& v.val1< < v.val2;
}

除此之外,我没有在Visual Studio下编译代码的问题或警告2008年,在第4级有警告。哦,有传统的链接器错误,但是很容易通过将模板函数定义移动到类声明旁路,如 C++常见问题



我的测试代码:

  #include< iostream> 
using namespace std;

template< class T,class U>
class Pair {
public:
Pair(T v1,U v2):val1(v1),val2(v2){}
〜Pair(){}
对& operator =(const Pair&);
friend ostream&运算符<<<(ostream& out,Pair< T,U>& v)
{
return out& v.val1<< < v.val2;
}
private:
T val1;
U val2;
};

int main(){
Pair< int,int> a(3,4);
cout<<一个;
}


I'm trying to overload the operator << as a friend to a template class Pair, but I keep getting a compiler warning saying

friend declaration std::ostream& operator<<(ostream& out, Pair<T,U>& v) declares a non template function

for this code:

friend ostream& operator<<(ostream&, Pair<T,U>&);

it gives a second warning as a recommendation saying

if this is not what you intended, make sure the function template has already been declared and add <> after the function name here

Here is the function definition

template <class T, class U>
ostream& operator<<(ostream& out, Pair<T,U>& v)
{
    out << v.val1 << " " << v.val2;
}

and here is the whole class.

template <class T, class U>
class Pair{
public:
    Pair(T v1, U v2) : val1(v1), val2(v2){}
    ~Pair(){}
    Pair& operator=(const Pair&);
    friend ostream& operator<<(ostream&, Pair<T,U>&);

private:
    T val1;
    U val2;
};

I wasn't sure what to draw from the recommendation warning, other than that maybe I have to put somewhere in the friend declaration. Does anyone know the proper syntax for this? Thanks.

解决方案

You declare operator<< as returning an ostream&, but there is no return statement at all in the method. Should be:

template <class T, class U>
ostream& operator<<(ostream& out, Pair<T,U>& v)
{
    return out << v.val1 << " " << v.val2;
}

Other than that, I have no problems or warnings compiling your code under Visual Studio 2008 with warnings at level 4. Oh, there are the classical linker errors, but that is easily bypassed by moving the template function definition to the class declaration, as explained in the C++ FAQ.

My test code:

#include <iostream>
using namespace std;

template <class T, class U>
class Pair{ 
public:
    Pair(T v1, U v2) : val1(v1), val2(v2){}
    ~Pair(){}
    Pair& operator=(const Pair&);
    friend ostream& operator<<(ostream& out, Pair<T,U>& v)
    {
        return out << v.val1 << " " << v.val2;
    }
private:    
    T val1;
    U val2;
};

int main() {
    Pair<int, int> a(3, 4);
    cout << a;      
}

这篇关于重载朋友运算符&lt;&lt;为模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆