【发布时间】:2016-08-27 14:08:51
【问题描述】:
我正在尝试在模板类中获取一个友元函数以进行编译,但错误消息和警告我不明白。我已经演示了这个问题。我得到的错误是:
prog.cpp:8:57: error: non-class, non-variable partial specialization C operator+(const B& lhs, const C& rhs);
prog.cpp:15:59: 警告:友元声明 'C operator+(const B&, const C&)' 声明了一个非模板函数 [-Wnon-template-friend] 朋友 C 运算符+(const B& lhs, const C& rhs);
prog.cpp:15:59: 注意:(如果这不是您想要的,请确保函数模板已经声明并在此处的函数名称后添加 )
#include <iostream>
using namespace std;
template<typename A, typename B>
class C;
template<typename A, typename B>
C<A, B> operator+<A, B>(const B& lhs, const C<A, B>& rhs);
template<typename A, typename B>
struct C
{
A val_;
C operator+(const C& other) const;
friend C<A, B> operator+(const B& lhs, const C<A, B>& rhs);
};
template<typename A, typename B>
C<A, B> C<A, B>::operator+(const C<A, B>& other) const
{
C<A, B> c;
c.val_ = this->val_ + other.val_;
return c;
}
template<typename A, typename B>
C<A, B> operator+(const B& lhs, const C<A, B>& rhs)
{
C<A, B> c;
c.val_ = lhs + rhs.val_;
return c;
}
int main()
{
C<string, char> c0,c1;
c0.val_ = " C0 ";
c1.val_ = " C1 ";
cout << "Stuct:" << (c0 + c1).val_ << '\n';
cout << "Friend:" << ('~' + c1).val_ << endl;
return 0;
}
【问题讨论】:
-
你不需要
operator+的第一个声明,这是错误的。 -
这是问题的一个例子。我的实际代码中需要三种形式:C 运算符+(const C& other)、C 运算符+(const B& other)和 C 运算符(const B& lhs、const C& rhs)。注:不是实际的编译代码,只是需要的一般形式。
标签: c++ templates c++11 friend