Search code examples
c++classfunctionfriend

what does undefined reference to operator<<.... means? in c++


I have friend function in the class. (in header file. head.h) i have implemented in head.cpp and in head.h outside of the class i have ostream& operator<< (ostream& out, TreeNode* ptr); i implemented in head.cpp as well. Is there any problem with that?

Header file

friend ostream& operator<< (ostream& out, const TreeDB& ptr);
 ostream& operator<< (ostream& out, TreeNode* ptr);

Implementation

ostream& operator<< (ostream& out, TreeNode* ptr)
{
 if(rhs!=NULL)
 {
  operator<<(out,ptr->Left());
  out<<(*(ptr->Entry()));
  operator<<(out,ptr->Right());
 }
 return out;
}




ostream& operator<< (ostream& out, const TreeDB& ptr)
{
 return (operator<<(out,ptr.root));
}

Error: undefined reference to 'operator<<(std::basic_ostream >&,DBentry const&)'

DBentry is another class which manipulates the database entry


Solution

  • When you got an undefined reference it is often an symptom of either a missing function definition, a small difference in signature from the definition and the declaration or that the definition of the function is not compiled/linked in.

    So check that your arguments match, that the namespaces match, and that the definition of the function indeed is compiled.

    As far as I can see in the code you have provided you have not defined an operator<< for DBEntry& const.