我试图超载< <。到目前为止没有运气。 这里是我的过载实现:未能超载运算符<<(C++)
struct Engineer{
int id;
int salary;
bool hired;
public:
Engineer(int _id, int _salary) : id(_id), salary(_salary), hired(false) {}
std::ostream& operator<<(std::ostream& os)
{
return os << " " << id << std::endl;
}
};
,这里是我尝试使用它:
void inorderTravel(AvlNode* root) {
if(root == NULL) return;
inorderTravel(root->left);
std::cout << root->data; // <- here
inorderTravel(root->right);
}
行了 “的std ::法院< <根 - >数据;”唤起所有错误:
> Multiple markers at this line
> - cannot convert 'root->AvlTree<Engineer, IdKey>::AvlNode::data' (type 'Engineer') to type 'unsigned char'
> - cannot convert 'root->AvlTree<Engineer, IdKey>::AvlNode::data' (type 'Engineer') to type 'signed char'
> - 'Engineer' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
> - cannot convert 'root->AvlTree<Engineer, IdKey>::AvlNode::data' (type 'Engineer') to type 'char'
> - deduced conflicting types for parameter '_CharT' ('char' and 'Engineer')
> - no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Engineer')
> - candidates are:
> - cannot convert 'root->AvlTree<Engineer, IdKey>::AvlNode::data' (type 'Engineer') to type 'const char*'
> - mismatched types 'const _CharT*' and 'Engineer'
> - cannot convert 'root->AvlTree<Engineer, IdKey>::AvlNode::data' (type 'Engineer') to type 'const unsigned char*'
> - cannot convert 'root->AvlTree<Engineer, IdKey>::AvlNode::data' (type 'Engineer') to type 'const signed char*'
什么是'AvlNode'的定义是什么? –
什么是'root-> data'? – 0x499602D2
[运算符重载]的可能重复(http://stackoverflow.com/questions/4421706/operator-overloading) –