【发布时间】:2019-05-22 14:48:22
【问题描述】:
当我尝试编译以下(截断为下面的小sn-p)代码时,
#include <iostream>
using namespace std;
template <typename value_type>
class Tree {
public:
Tree();
~Tree();
};
template <typename value_type>
const std::ostream& operator<<(const std::ostream& o, const Tree<value_type>& t) {
return o;
}
int main() {
Tree<int> tree;
cout << tree << endl;
}
我收到以下错误:
clang 在 Mac 上
error: reference to overloaded function could not be resolved;
did you mean to call it?
cout << tree << endl;
^~~~
gnu gcc 在 debian linux 上
error: no match for 'operator<<'
(operand types are
'const ostream {aka const std::basic_ostream<char>}'
and '<unresolved overloaded function type>')
cout << tree << endl;
~~~~~~~~~~~~~^~~~~~~
如果我从不实现运算符重载,则 gnu g++ 会给我以下错误:
error: no match for 'operator<<'
(operand types are
'std::ostream {aka std::basic_ostream<char>}'
and 'Tree<int>')
cout << tree << endl;
~~~~~^~~~~~~
我实际上不明白我在这里做错了什么。我想做的就是能够像您一样将我的模板类通过管道传输到ostream。有什么想法吗?
【问题讨论】:
标签: c++ templates compiler-errors ostream c++98