I've been trying to code my own binary tree (without looking at any examples), but for some reason my code just refuses to compile! here is my source:
It all seems to check out fine, yet I get the following errors from g++:Code:#include <iostream.h> struct node { node *right; node *left; int key; }; typedef node* NodePtr; void add_node(NodePtr &tree, int value, int switch); main() { int num, num_of_nodes; NodePtr root, pointer; int called = 0; cout << "How many numbers do you whish to add to the tree?"; cin >> num_of_nodes; cout << "Enter the first node:"; cin >> num; root->key = num; pointer = root; for (int i = 0; i < num_of_nodes; i++) { cout << "Enter a number to add:"; cin >> num; add_node(pointer, num, called); pointer = root; called = 0; } return 0; } void add_node(NodePtr &tree, int value, int switch) { if (switch == 0) { node* tmp = new node; tmp->key = value; switch = 1; } if (value >= tree->key) if (tree->right != NULL) { tree = tree->right; add_node(tree, value); } else tree->right = tmp; if (value <= tree->key) if (tree->left != NULL) { tree = tree->left; add_node(tree, value); } else tree->left = tmp; }
WHY!?Code:binary_tree.cpp:11: parse error before `switch' binary_tree.cpp:35: parse error before `switch' binary_tree.cpp: In function `void add_node(...)': binary_tree.cpp:37: parse error before `switch' binary_tree.cpp:40: `tmp' undeclared (first use this function) binary_tree.cpp:40: (Each undeclared identifier is reported only once for each function it appears in.) binary_tree.cpp:40: `value' undeclared (first use this function) binary_tree.cpp:41: parse error before `=' token binary_tree.cpp: At global scope: binary_tree.cpp:47: `tree' was not declared in this scope binary_tree.cpp:47: ISO C++ forbids declaration of `add_node' with no type binary_tree.cpp:47: `int add_node' redeclared as different kind of symbol binary_tree.cpp:36: previous declaration of `void add_node(...)' binary_tree.cpp:47: initializer list being treated as compound expression binary_tree.cpp:48: parse error before `}' token binary_tree.cpp:56: `tree' was not declared in this scope binary_tree.cpp:56: ISO C++ forbids declaration of `add_node' with no type binary_tree.cpp:56: redefinition of `int add_node' binary_tree.cpp:47: `int add_node' previously defined here binary_tree.cpp:56: initializer list being treated as compound expression binary_tree.cpp:57: parse error before `}' token![]()



LinkBack URL
About LinkBacks



