类模板上的运算符重载
我在为模板类定义一些运算符重载时遇到一些问题。让我们以这个假设的类为例。
template <class T>
class MyClass {
// ...
};
operator+=
// In MyClass.h
MyClass<T>& operator+=(const MyClass<T>& classObj);
// In MyClass.cpp
template <class T>
MyClass<T>& MyClass<T>::operator+=(const MyClass<T>& classObj) {
// ...
return *this;
}
导致此编译器错误:
no match for 'operator+=' in 'classObj2 += classObj1'
operator<<
// In MyClass.h
friend std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj);
// In MyClass.cpp
template <class T>
std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj) {
// ...
return out;
}
导致此编译器警告:
friend declaration 'std::ostream& operator<<(std::ostream&, const MyClass<T>&)' declares a non-template function
我在这里做错了什么?
I'm having some problems defining some operator overloads for template classes. Let's take this hypothetical class for example.
template <class T>
class MyClass {
// ...
};
operator+=
// In MyClass.h
MyClass<T>& operator+=(const MyClass<T>& classObj);
// In MyClass.cpp
template <class T>
MyClass<T>& MyClass<T>::operator+=(const MyClass<T>& classObj) {
// ...
return *this;
}
Results in this compiler error:
no match for 'operator+=' in 'classObj2 += classObj1'
operator<<
// In MyClass.h
friend std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj);
// In MyClass.cpp
template <class T>
std::ostream& operator<<(std::ostream& out, const MyClass<T>& classObj) {
// ...
return out;
}
Results in this compiler warning:
friend declaration 'std::ostream& operator<<(std::ostream&, const MyClass<T>&)' declares a non-template function
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要说出以下内容(因为您与整个模板成为朋友,而不仅仅是它的专门化,在这种情况下,您只需要在后面添加一个
<>
operator<<
):实际上,除非它访问私有或受保护的成员,否则不需要将其声明为友元。由于您刚刚收到警告,看来您的友谊宣言并不是一个好主意。如果您只想将其声明为友元,可以像下面所示的那样,在类之前声明模板的单一特化,以便
operator<<
被识别为模板。上述和这种方式都将其特化声明为友元,但第一种将所有特化声明为友元,而第二种仅将
operator<<
的特化声明为朋友,其T
等于授予友谊的类的T
。在另一种情况下,您的声明看起来不错,但请注意,您不能将
+=
将MyClass
转换为MyClass
当T
和U
与该声明的类型不同时(除非您在这些类型之间有隐式转换)。您可以将您的+=
设为会员模板You need to say the following (since you befriend a whole template instead of just a specialization of it, in which case you would just need to add a
<>
after theoperator<<
):Actually, there is no need to declare it as a friend unless it accesses private or protected members. Since you just get a warning, it appears your declaration of friendship is not a good idea. If you just want to declare a single specialization of it as a friend, you can do that like shown below, with a forward declaration of the template before your class, so that
operator<<
is regognized as a template.Both the above and this way declare specializations of it as friends, but the first declares all specializations as friends, while the second only declares the specialization of
operator<<
as a friend whoseT
is equal to theT
of the class granting friendship.And in the other case, your declaration looks OK, but note that you cannot
+=
aMyClass<T>
to aMyClass<U>
whenT
andU
are different type with that declaration (unless you have an implicit conversion between those types). You can make your+=
a member template这对于模板无效。运算符的完整源代码必须位于使用它的所有翻译单元中。这通常意味着代码内联在标头中。
编辑:从技术上讲,根据标准,可以导出模板,但很少有编译器支持它。此外,如果模板在 MyClass.cpp 中为所有 T 类型显式实例化,您也可以执行上述操作,但实际上,这通常违背了模板的要点。
更多编辑:我通读了你的代码,它需要一些工作,例如重载运算符[]。此外,通常,我会将维度作为模板参数的一部分,允许在编译时捕获 + 或 += 的失败,并允许对类型进行有意义的堆栈分配。您的异常类还需要从 std::exception 派生。然而,这些都不涉及编译时错误,它们只是不是很好的代码。
This is invalid for templates. The full source code of the operator must be in all translation units that it is used in. This typically means that the code is inline in the header.
Edit: Technically, according to the Standard, it is possible to export templates, however very few compilers support it. In addition, you CAN also do the above if the template is explicitly instantiated in MyClass.cpp for all types that are T- but in reality, that normally defies the point of a template.
More edit: I read through your code, and it needs some work, for example overloading operator[]. In addition, typically, I would make the dimensions part of the template parameters, allowing for the failure of + or += to be caught at compile-time, and allowing the type to be meaningfully stack allocated. Your exception class also needs to derive from std::exception. However, none of those involve compile-time errors, they're just not great code.
这帮助我解决了完全相同的问题。
解决方案:
在
class
本身的定义之前转发声明friend
函数。例如:在类中使用“<>”声明您的友元函数附加到函数名称后。
This helped me with the exact same problem.
Solution:
Forward declare the
friend
function before the definition of theclass
itself. For example:Declare your friend function in your class with "<>" appended to the function name.
您必须指定该友元是模板函数:
请参阅此 C++ FAQ Lite 答案以获取详细信息。
You must specify that the friend is a template function:
See this C++ FAQ Lite answer for details.
这种方式有效:
This way works: