[英]Templated Class Friend Operator Member Function
I'm trying to get a friend function inside a templated class to compile, but the error message and warning I do not understand. 我正在尝试在模板化的类中获取一个朋友函数来编译,但错误消息和警告我不明白。 I've made a demonstration of the issue.
我已经证明了这个问题。 The error I'm getting is:
我得到的错误是:
prog.cpp:8:57: error: non-class, non-variable partial specialization C operator+(const B& lhs, const C& rhs);
prog.cpp:8:57:错误:非类,非可变部分特化C运算符+(const B&lhs,const C&rhs);
prog.cpp:15:59: warning: friend declaration 'C operator+(const B&, const C&)' declares a non-template function [-Wnon-template-friend] friend C operator+(const B& lhs, const C& rhs);
prog.cpp:15:59:警告:朋友声明'C运算符+(const B&,const C&)'声明一个非模板函数[-Wnon-template-friend]朋友C运算符+(const B&lhs,const C&rhs);
prog.cpp:15:59: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)
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;
}
The simplest is to inline code inside the class: 最简单的是在类中内联代码:
template <typename A, typename B>
struct C
{
A val_;
C operator+(const C& other) const
{
C c;
c.val_ = this->val_ + other.val_;
return c;
}
friend C operator+ (const B& lhs, const C& rhs)
{
C c;
c.val_ = lhs + rhs.val_;
return c;
}
};
The code not inlined in the class, which requires lot of attention as forward declaration order of declaration, strange syntax <>
: 代码没有内联在类中,需要很多关注作为声明的前向声明顺序, 奇怪的语法
<>
:
template <typename A, typename B> struct C;
template <typename A, typename B>
C<A, B> operator+ (const B& lhs, const C<A, B>& rhs);
template <typename A, typename B>
struct C
{
A val_;
friend C<A, B> operator+<> (const B& lhs, const C<A, B>& rhs);
C operator+(const C& other) const;
};
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;
}
template <typename A, typename B>
C<A, B> C::operator+(const C<A, B>& other) const
{
C<A, B> c;
c.val_ = this->val_ + other.val_;
return c;
}
This declaration: 本声明:
template<typename A, typename B>
C<A, B> operator+<A, B>(const B& lhs, const C<A, B>& rhs);
...is wrong because of the <A,B>
between operator+
and (
, I don't really know what you wanted to do here. You would use this form if you were to specialize a templated operator+
, but you are not here, you are overloading one. ...是错误的,因为
operator+
和之间的<A,B>
(
我真的不知道你想在这里做什么。如果你要专门化一个模板化operator+
,你会使用这个表格,但你不在这里,你正在超载一个。
This declaration should be: 该声明应为:
template<typename A, typename B>
C<A, B> operator+ (const B& lhs, const C<A, B>& rhs);
Then you should explicitely specify in your friend
declaration that you want a specialized version by writting: 然后你应该在你的
friend
声明中明确指出你想要一个专门的版本通过写:
friend C<A,B> operator+<>(const B& lhs, const C<A,B>& rhs);
You need to put this before your operator+
, otherwize the compiler will think this is a specialization of a non-templated function. 你需要在
operator+
之前放置它,否则编译器会认为这是非模板化函数的特化。
Anyway, if you have no real reason to put your code outside the C
class, I would go @Jarod42 solution. 无论如何,如果你没有真正的理由将你的代码放在
C
类之外,我会去@ Jarod42解决方案。
Your whole code should look like this: 您的整个代码应如下所示:
// Declaration of struct C with delayed definition
template <typename A, typename B>
struct C;
// Initial declaration of templated operator+
template <typename A, typename B>
C<A, B> operator+ (const B&, const C<A, B>&);
// Definition of C
template <typename A, typename B>
struct C {
friend C operator+<> (const B&, const C&);
// This must be AFTER the templated operator+
C operator+ (const C&) const;
};
template<typename A, typename B>
C<A, B> C<A, B>::operator+(const C<A, B>& other) const {
}
template<typename A, typename B>
C<A, B> operator+(const B& lhs, const C<A, B>& rhs) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.