compile error

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
gijs
New poster
Posts: 3
Joined: Tue Feb 21, 2006 12:40 pm

compile error

Post by gijs » Tue Feb 21, 2006 12:45 pm

The following code results in a compile error, anyone got a clue why this happens?

#include <iostream>
using namespace std;

class TreeNode {
public:
int value;
TreeNode *left, *right;
TreeNode(int value) {
this->value = value;
}
bool contains(int total, int help) {
if(left == NULL && right == NULL) {
return total == help+value;
}
return ((left != NULL && left->contains(total, help+value))
|| (right != NULL && right->contains(total, help+value)));
}
};

int nextToken() {
while(!cin.eof()) {
char next = cin.peek();
if(next == '(' || next == ')') {
return cin.get();
} else if ((next >= '0' && next <= '9') || next == '-') {
int ret;
cin >> ret;
return ret;
}
cin.get();
}
exit(0);
}

TreeNode* read() {
int next = nextToken();
if(next != '(') {
cout << "Expected \'(\', but got \'" << next << "\'\n";
exit(1);
}
next = nextToken();
if(next == ')') { // empty tree ()
return NULL;
} else { // non-empty tree (int tree tree)
TreeNode *node = new TreeNode(next);
node->value = next;
node->left = read();
node->right = read();
next = nextToken();
if(next != ')') {
cout << "Expected \')\', but got \'" << (char)next << "\'\n";
exit(1);
}
return node;
}
}

int main() {
int seek;
while(!cin.eof()) {
cin >> seek;
cout << "Seeking " <<seek<<" ... ";
TreeNode* node = read();
string result = (node!=NULL && node->contains(seek, 0))?"yes":"no";
cout << result;
cout<<"\n";
}
return 0;
}

misof
A great helper
Posts: 430
Joined: Wed Jun 09, 2004 1:31 pm

Post by misof » Tue Feb 21, 2006 1:35 pm

You need to #include <string>

gijs
New poster
Posts: 3
Joined: Tue Feb 21, 2006 12:40 pm

Post by gijs » Tue Feb 21, 2006 1:41 pm

It worked! Thanks a lot..

Post Reply

Return to “C++”