决策搜索树中的链接错误2019和1120



我一直得到这个编译器错误。我尝试将类节点更改为结构,但这似乎没有帮助。我知道我的btree类现在都是公共的,我还没有创建一个geroot函数来访问btree的私有数据成员。

h。文件:

#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文件:

#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);
}
演示文件:

#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;
}

嗯,我不太确定,因为它是一段时间以来,我上次使用c++,但你不应该实现节点头文件太?特别是构造函数,因为我看到原型在那里,但我没有看到任何定义。你能指出错误的确切位置吗?

最新更新