Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法将模板类管道化到cout_C++_Templates_Compiler Errors_Ostream_C++98 - Fatal编程技术网

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;
}