C ++无法将Type * const转换为Type *&

时间:2016-05-12 22:11:54

标签: c++ pointers reference const lnk

我一直试图找到一个错误消息的解决方案,当我尝试将一个私有对象指针从WordTree传递到我的<<中的一个递归数组时过载功能。

部首:

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 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

我应该如何实现我的代码,以便访问数据成员的ptr可以工作,同时确保我的WordTree的根目录正确传递?

0 个答案:

没有答案