模板函数中的运算符<<重载
operator << overloading in a template function
我无法判断这段代码的哪一部分是错误的。下面给出了错误消息。
我想重载<<
运算符,以便我可以像cout << tree
一样编写代码。我查找了有关模板,友元函数,运算符重载的信息。但我仍然不明白为什么会出错。
template <typename Value>
class Tree {
protected:
Node<Value>* root = NULL;
int size = 0;
std::ostream& _ostreamOperatorHelp(Node<Value>* node, int level,
std::ostream& os) {
...
}
public:
friend std::ostream& operator<< <Value>(std::ostream& os,
Tree<Value> const& tree);
};
template <typename Value>
std::ostream& operator<<(std::ostream& os, Tree<Value> const& tree) {
tree._ostreamOperatorHelp(tree.GetRoot(), 0, os);
return os;
}
错误信息:
Tree.hpp:129:34: error: declaration of 'operator<<' as non-function
friend std::ostream& operator<< <Value>(std::ostream& ,
^~
在与特定的模板专用化交朋友之前,您必须先像这样声明通用模板函数:
template <typename Value>
class Tree;
template <typename Value>
std::ostream& operator<<(std::ostream& os, Tree<Value> const& tree);
template <typename Value>
class Tree {
protected:
Node<Value>* root = NULL;
int size = 0;
std::ostream& _ostreamOperatorHelp(Node<Value>* node, int level,
std::ostream& os) {
...
}
public:
friend std::ostream& operator<< <Value>(std::ostream& os,
Tree<Value> const& tree);
};
template <typename Value>
std::ostream& operator<<(std::ostream& os, Tree<Value> const& tree) {
tree._ostreamOperatorHelp(tree.GetRoot(), 0, os);
return os;
}
相关文章:
- C++重载<<具有typedef'd std::vector
- 重载运算符<<:此运算符函数的参数太多
- 在VS 2012上编译,但不使用Netbeans/MinGW-重载<<
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员
- C++分离成员流重载<<
- 我已经完成了<<运算符重载,但它'It’不起作用
- 重载运算符<<输出地址而不是数据成员
- 重载运算符<<用于ostream语法
- 重载operator< & lt;作为会员打印
- Operator< & lt;重载隐藏其他
- 重载& lt; & lt;为什么我得到以下错误
- 重载ostream&时转换错误无效;operator< & lt;
- operator< & lt;在c++中为类中的类重载
- 重载& lt; & lt;用于打印自定义异常的操作符
- 重载<<运算符错误C2804:二进制'运算符<<'参数太多
- 上Ostream & lt; & lt;重载的困惑
- 重载& lt; & lt;为了我的班级
- 使用带有重载<<endl上的运算符
- 重载& lt; & lt;使用命名空间std
- operator< & lt;重载、名称空间和模板