【发布时间】:2011-05-01 16:31:15
【问题描述】:
我有一个类似于下面代码的基类。我正在尝试重载
base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base<T>*)’ declares a non-template function
base.h:24: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
我尝试在类声明/原型中的 。但是,然后我得到它does not match any template declaration。我一直在尝试将运算符定义完全模板化(我想要),但我只能让它与以下代码一起使用,并手动实例化运算符。
base.h
template <typename T>
class Base {
public:
friend ostream& operator << (ostream &out, Base<T> *e);
};
base.cpp
ostream& operator<< (ostream &out, Base<int> *e) {
out << e->data;
return out;
}
我只想在标头 base.h 中包含这个或类似内容:
template <typename T>
class Base {
public:
friend ostream& operator << (ostream &out, Base<T> *e);
};
template <typename T>
ostream& operator<< (ostream &out, Base<T> *e) {
out << e->data;
return out;
}
我在网上其他地方读到,在原型中将 放在
【问题讨论】:
-
这正是 Dan Saks 的"Making New Friends" idiom 解决的问题。 (对不起,迟到的评论。)
-
我已经链接到另一个问题的答案,该答案详细解释了为什么建议的修复是必要的/工作
标签: c++ templates operator-overloading friend specialization