C++ 无法将模板类管道化到cout
当我试图编译以下代码(被截断为下面的小片段)时C++ 无法将模板类管道化到cout,c++,templates,compiler-errors,ostream,c++98,C++,Templates,Compiler Errors,Ostream,C++98,当我试图编译以下代码(被截断为下面的小片段)时 #包括 使用名称空间std; 模板 类树{ 公众: 树(); ~Tree(); }; 模板 const std::ostream&operator删除std::ostream上的consts-您不能将const流用于任何事情 template <typename value_type> std::ostream& operator<<(std::ostream& o, const Tree<value_
#包括
使用名称空间std;
模板
类树{
公众:
树();
~Tree();
};
模板
const std::ostream&operator删除std::ostream
上的const
s-您不能将const流用于任何事情
template <typename value_type>
std::ostream& operator<<(std::ostream& o, const Tree<value_type>& t) {
return o;
}
模板
std::ostream&operator出于某种原因,我在过去做过这件事,它工作得很好,但现在我的编译器选择只接受非常量。@finrayment您可能用其他运算符做过,但我怀疑是否有任何编译器被破坏得足以让您用operator诚实地说,我发誓我是用operator@finnrayment对于std::cout@finnrayment来说这是“很好的”,我经常陷入这个陷阱,忘记返回流,忽略警告,直到我使用链接,一切看起来都很好。。。
error: reference to overloaded function could not be resolved;
did you mean to call it?
cout << tree << endl;
^~~~
error: no match for 'operator<<'
(operand types are
'const ostream {aka const std::basic_ostream<char>}'
and '<unresolved overloaded function type>')
cout << tree << endl;
~~~~~~~~~~~~~^~~~~~~
error: no match for 'operator<<'
(operand types are
'std::ostream {aka std::basic_ostream<char>}'
and 'Tree<int>')
cout << tree << endl;
~~~~~^~~~~~~
template <typename value_type>
std::ostream& operator<<(std::ostream& o, const Tree<value_type>& t) {
return o;
}