0

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

fhuseynli
  • 630
  • 3
  • 12
  • 26
  • 6
    Why don't you show us your actual code instead of describing it? – NPE Nov 22 '12 at 09:26
  • 1
    You get "undefined reference to..." error messages from the linker, and it can indicate that you miss a file when linking your project. – Some programmer dude Nov 22 '12 at 09:27
  • because i don't think this error occurs because of logic error, its something about declaration part – fhuseynli Nov 22 '12 at 09:28
  • It is still easier to quickly assess how the declarations and definitions relate if you see them written out, rather than have them described in lengthy natural language. – Agentlien Nov 22 '12 at 09:29
  • when i compile my files seperately, no error occurs, but when i combine them it writes undefined reference – fhuseynli Nov 22 '12 at 09:29
  • 1
    "Is there any problem with that?" ... Is there any problem with *what*? .. "its something about declaration part" - then show us the declarations and their points of usage. If you want help, oblige the ones that can help you. While you're at it the **exact** error message please. – WhozCraig Nov 22 '12 at 09:34
  • possible duplicate of [undefined reference to operator<<](http://stackoverflow.com/questions/10712380/undefined-reference-to-operator) – finnw Nov 22 '12 at 12:16

2 Answers2

2

Your global function for TreeDB printing is friended in your class definition (we're assuming) as:

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

You later define it as

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

Notice: ptr is const; therefore ptr.root is const as well

Now look at your node pointer operator:

ostream& operator<< (ostream& out, TreeNode* ptr)

ptr is not const, and therefore this operator does not apply. it should be:

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

Once you fix that. your error message:

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

This is almost guaranteed to be because you don't have the following:

ostream& operator <<(ostream& os, const DBEntry& entry);

It is highly likely if you do have an operator like this it was not declared with a const qualifier on the right-hand-side in the same way you didn't declare const TreeNode* correctly in its operator <<().

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • i added const but it shows same error again. – fhuseynli Nov 22 '12 at 09:54
  • now it compiled but output from my program is 0x9bc61380x9bc61000x9bc6158> – fhuseynli Nov 22 '12 at 10:11
  • Those would be pointer values. At least you're making progress. Personally I would ditch all operators except the const TreeDB& and maybe DBEntry operator, instead implementing `write(ostream&) const;` members for each class that needs to dump to an output stream, but your compilation error is fixed, so thats a good thing. – WhozCraig Nov 22 '12 at 10:18
  • this because out<<(*(rhs->getEntry())) when i delete the star it prints pointer not what stored at that pointer. how can i fix it? – fhuseynli Nov 22 '12 at 10:29
  • 1
    @user1722022 Don't delete the *, and make damn sure Entry() isn't null before calling that operator.. – WhozCraig Nov 22 '12 at 10:30
1

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.

daramarak
  • 6,115
  • 1
  • 31
  • 50
  • friend ostream& operator<< (ostream& out, const TreeDB& rhs);ostream& operator<< (ostream& out, TreeNode* rhs); these are in my header file – fhuseynli Nov 22 '12 at 09:32
  • and these are my implementation part ostream& operator<< (ostream& out, TreeNode* rhs) { if(rhs!=NULL) { operator<<(out,rhs->getLeft()); out<<(*(rhs->getEntry())); operator<<(out,rhs->getRight()); } return out; } ostream& operator<< (ostream& out, const TreeDB& rhs) { return (operator<<(out,rhs.root)); } – fhuseynli Nov 22 '12 at 09:32
  • 1
    @user1722022 update your code to your question instead of as comment in here – billz Nov 22 '12 at 09:33
  • check namespaces also, but first of all check the compiler/linker output that the cpp file in question is compiled and linked in. – daramarak Nov 22 '12 at 09:38
  • now it compiled but output from my program is 0x9bc61380x9bc61000x9bc6158> – fhuseynli Nov 22 '12 at 10:12
  • @user1722022 sounds like three pointers stuffed together. Isn't that what you want? – daramarak Nov 22 '12 at 10:25
  • no, i dont want to print pointers, i want to print the data which stored at that pointers. – fhuseynli Nov 22 '12 at 10:30
  • @user1722022 then you probably want to use the * operator on the pointers, after you check them for nullptr. – daramarak Nov 22 '12 at 10:39
  • @user1722022 glad to be of help. Next time give more details when you ask, then you'll probably get less flac :) – daramarak Nov 22 '12 at 11:04