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