cout help

How do i get this to work?

void BSTree::printInorder(Node* node)
{
if(node!=nullptr)
{
this->printInorder(node->Left());
cout << node->Key2();
this->printInorder(node->Right());
}
}


the error is my "<<" after the cout does not match the operands.

Key2(); is Person_Info Node::Key2()
Person_Info is another class that i'm using
Last edited on
You need overloading the 'operator<<' function which handles the ostream. That means in your case that you need to implement std::ostream& operator<< ( std::ostream&, const Person_Info& )

Here an example how you could do it. If e.g. Person_Info would be:
1
2
3
4
5
class Person_Info {
public:
    string name;
    int age;
}

Then your overloaded operator<< function should be look like:
1
2
3
4
5
6
std::ostream & operator<< ( std::ostream & ostr, const Person_Info & p_i)
{
    ostr << "name: " << p_i.name << '\n'
         << "age: " << p_i.age << '\n';
    return ostr;
}

Don't forget the '&'s at the method signature, which means that all objects will be handled as a reference.

Now you could write: std::cout << person_info; if person_info is an object of class Person_Info.
Topic archived. No new replies allowed.