I'm trying to overload my << operator, but I want to use a helper function because I'm working with a tree, and that way my helper fn. can be recursive. But when I try to call the helper from the operator function I'm getting this no matching function call error.
std::ostream& operator<<(std::ostream& out, const Polynomial &rhs)
{
Polynomial::Node *p = rhs.root;
rhs.printPoly(p, out);
return out;
}
void Polynomial::printPoly(Node* p, std::ostream &out)
{
if(p == nullptr)
return;
printPoly(p->left, out);
out << p->item->coeff() << "x^" << p->item->x();
printPoly(p->right, out);
}
and in the .h file
friend std::ostream& operator<<(std::ostream& out, const Polynomial& rhs);
Oh and here's the error:
no matching function for call to 'Polynomial::printPoly(Polynomial::Node*&, std::ostream&) const
'
Add const
to the end of your function declaration:
void Polynomial::printPoly(Node* p, std::ostream &out) const
{
...
}
This extra const
tells the compiler that you won't be modifying the Polynomial
object in the printPoly
method.