0

I keep getting this compiler error. I tried changing the class node to a struct but that didn't seem to help. I am aware that my btree class right now is all public I didn't create a getroot function yet to access the btree's private data members.

h. file:

#ifndef BTREE_H
#define BTREE_H

#include <fstream>
#include <string>
#include <iostream>

using namespace std;

class node {

    node& operator = (node* nd1)
    { this->left = nd1->left;
     this->right = nd1->right;
     this->data = nd1->data;
     return *this;}

public:
    node(const std::string data);
    std:: string data;
    node *left, *right;

};

class btree {
public:
    //class node;
    btree();
    void addRoot(const std::string d);
    void addLeft(node *nd, const std::string &data);
    void addRight(node *nd, const std::string &data);


    //node *getRoot();

    //std::string get(node *node);

public:
    //btree::
        node *root;

};

.cpp file:

#include <fstream>
#include <sstream>
#include <string>

using namespace std;

btree::btree(){
    root=NULL;}

void btree::addRoot(const std:: string d) {root = new node(d);}



void btree::addLeft(node *nd, const string &data) {
    if (nd == 0) throw "Attempt addLeft on null";
    if (nd->left != 0) throw "Attempt addLeft on nd with existing left child";
    nd->left = new node(data);}



void btree::addRight(node *nd, const string &data) {
    if (nd == 0) throw "Attempt addRight on null";
    if (nd->right != 0) throw "Attempt addRight on nd with existing right child";
    nd->right = new node(data);
}

demo file:

#include "btree.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>


using namespace std;

int main(){
    char answer;
    string question, animal;

    btree game;
    game.addRoot("Elephant");
    bool again=true;
    bool solved;
    solved=false;

    while(!solved){

        while(again){
            cout<<game.root<<"?"<<endl;
            cout<<"Y/N"<<endl;
            cin>>answer;
                    if( answer == 'y' || 'Y'){

                                if(!(game.root->left)){
                                        cout<<"Game over. Computers are smart!!! ;)"<<endl;
                                        solved=true;
                                        again = false;}
                                    else
                                            game.root= game.root->left;
                                            again = true;
                    }
                        else 
                                //user clicked no check if leaf to the right 
                                    if (!(game.root->right)){
                                    cout<<"No more answers, You WIN!!!!"<<endl;
                                    cout<<"What question could I have asked instead?"<<endl;
                                    cin>>question;

                                    string temp= game.root->data;
                                    game.root->data= question;

                                    cout<<"What is the animal?"<<endl;
                                    cin>>animal;

                                    game.addLeft(game.root, animal);
                                    game.addRight(game.root, temp);


                                    solved=true;
                                    again = false;}


                                            else{
                                                node *move= game.root->right;
                                                game.root = move;

                                                solved=false;
                                                again= true;}
        }
        }


return 0;
}
Tammytee
  • 592
  • 5
  • 9
  • The details of the error message will tell you what you forgot. – Raymond Chen May 19 '14 at 02:48
  • 1> Generating Code... 1>btree.obj : error LNK2019: unresolved external symbol "public: __thiscall node::node(class std::basic_string,class std::allocator >)" (??0node@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "public: void __thiscall btree::addRoot(class std::basic_string,class std::allocator >)" (?addRoot@btree@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) I checked it over many times and still cant figure out the error – Tammytee May 19 '14 at 02:59
  • Rest of the error: 1>C:\Users\Geek\Documents\Visual Studio 2010\Projects\Guessing Game\Debug\Guessing Game.exe : fatal error LNK1120: 1 unresolved externals – Tammytee May 19 '14 at 02:59
  • `unresolved external symbol "public: __thiscall node::node` Where is the implementation of the `node` constructor? – PaulMcKenzie May 19 '14 at 03:23
  • [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix): [Declared but did not define a variable or function.](http://stackoverflow.com/a/12574403/902497) – Raymond Chen May 19 '14 at 03:49

1 Answers1

0

Well i'm not so sure about this since it's been a while since I last used C++ but shouldn't you be implementing the node header file too? Particularly the constructor since I see that the prototype is there but I don't see any definition. Also could you point out the exact line of the error?

Girauder
  • 165
  • 1
  • 12