2

I have been stuck trying to find a solution to an error message I have been getting when trying to pass in a private object pointer from WordTree into a recursive array inside my << overload function.

Header:

struct WordNode
{
    unsigned int count;
    std::string word;
    WordNode* left;
    WordNode* right;
};

class WordTree
{
public:
    WordTree() : root(nullptr) {};
    ~WordTree();
    friend std::ostream& operator <<(std::ostream&, const WordTree&);
    void intorder(std::ostream&, const WordNode*);    //Removed & from WordNode*
private:
    WordNode* root;
};

CPP:

void intorder(ostream&, const WordNode*);    //Was missing from original code

ostream& operator <<(ostream& ostr, const WordTree& tree)
{
    intorder(ostr, tree.root);
    return ostr;
}

void WordTree::intorder(ostream& o, const WordNode* ptr)    //Removed & from WordNode* for this example
{
    if(ptr == nullptr)
        return;
    intorder(o, ptr->left);
    o << ptr->word << " " << ptr->count << "\n";
    intorder(o, ptr->right);
}

Error:

Error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl intorder(class std::basic_ostream<char,struct std::char_traits<char> > &,struct WordNode *)" (?intorder@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@PAUWordNode@@@Z) referenced in function "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class WordTree const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVWordTree@@@Z)
Error LNK1120: 1 unresolved externals //The exe file

How should I implement my code so that the ptr accessing the data members would work while at the same time making sure that the root of my WordTree is passed properly?

J. Willus
  • 537
  • 1
  • 5
  • 15
  • Did it work after you removed the `&` from `WordNode*`? – R Sahu May 12 '16 at 22:22
  • 1
    Looks like you've got 2 `intorder`s. One we see, `WordTree::intorder`, and another, `intorder`, that we don't. The call in `operator <<` cannot be using the `WordTree::intorder` one – user4581301 May 12 '16 at 22:24
  • 1
    I don't believe that code produced that error message, given that the only `intorder` in your code has `void` return type. Post a [MCVE]. – Alan Stokes May 12 '16 at 22:24
  • 1
    @CaptainObvlious to be fair, the OP has reported the full error message, it's just that it is at the end of the second code block, formatted as code, and is therefore not very visible. – Fabio says Reinstate Monica May 12 '16 at 22:25
  • @RSahu The solution I'm trying to pin down assumes that already, it was causing some additional errors to my code. However, I'm not sure whether or not adding '&' back in addition to some other edits would help fix my code, so I left that comment there just in case I needed to go back. – J. Willus May 12 '16 at 22:26
  • We can't help much at the moment, I'm afraid. You've either left out some important bits or radically changed the code after getting that error message. Joining Alan Stokes in his quest for an MCVE. Without it this question will be placed on hold as unanswerable before long. – user4581301 May 12 '16 at 22:30
  • @user4581301 Ahh, I just noticed I had a function definition declared above the << overload. I removed the & from there as well. It's giving me a new link error. I'll update the code. – J. Willus May 12 '16 at 22:31
  • @J. Willus: When you had that `&` in the declaration, the code was obviously wrong and the error message was appropriate. Now, there's no such problem in the code anymore. So, what's your question then? – AnT stands with Russia May 12 '16 at 22:32
  • 3
    @J.Willus Wait! Before you edit your question, make sure you have a **reproducible** error, that is, a [mcve]. Please consider that if you don't prepare one, and keep trying something, and discover new errors and update the question accordingly, you make it unanswerable, because the question itself keeps changing. So try to isolate the problem and give us some code that you are not going to change. And especially, don't change it after some answers have been posted. If you have more than one question, ask more than one, don't keep updating the same one. Thank you! – Fabio says Reinstate Monica May 12 '16 at 22:36
  • If you have a link error, have a look at http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix. – Alan Stokes May 12 '16 at 22:37
  • Thanks for all of your help, guys, turns out it was a problem with intorder needing to be 1) called by the WordTree I passed in, 2) Making intorder const and 3) Removing the function definition at the top of my cpp file. – J. Willus May 12 '16 at 23:00

0 Answers0