我一直收到这个编译错误。我尝试将类节点更改为结构但这似乎没有帮助。我知道我现在的btree课程都是公开的,我还没有创建getroot功能来访问btree的私人数据成员。
小时。文件:
#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;
}
答案 0 :(得分:0)
我对此不太确定,因为自从我上次使用C ++以来已经有一段时间了,但是你也不应该实现节点头文件吗?特别是构造函数,因为我看到原型在那里,但我没有看到任何定义。你还可以指出错误的确切行吗?