Fin*_*ent 3 c++ templates compiler-errors ostream c++98
当我尝试编译以下代码(在下面的小片段中将其截断)时,
#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;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
铛在Mac
error: reference to overloaded function could not be resolved;
did you mean to call it?
cout << tree << endl;
^~~~
Run Code Online (Sandbox Code Playgroud)
Debian Linux上的gnu gcc
error: no match for 'operator<<'
(operand types are
'const ostream {aka const std::basic_ostream<char>}'
and '<unresolved overloaded function type>')
cout << tree << endl;
~~~~~~~~~~~~~^~~~~~~
Run Code Online (Sandbox Code Playgroud)
如果我从不实现运算符重载,则gnu g ++会给我以下错误:
error: no match for 'operator<<'
(operand types are
'std::ostream {aka std::basic_ostream<char>}'
and 'Tree<int>')
cout << tree << endl;
~~~~~^~~~~~~
Run Code Online (Sandbox Code Playgroud)
我实际上不明白我在这里做错了什么。我要做的就是能够ostream像您一样将我的模板类传递给。有任何想法吗?
删除consts- std::ostream您不能将const流用于任何内容。
template <typename value_type>
std::ostream& operator<<(std::ostream& o, const Tree<value_type>& t) {
return o;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33 次 |
| 最近记录: |