Hello all I'm trying to implement a tree in C++ with multiple children. There are compilers errors I'm trying to understand, but most likely it's implementation. I've looked at source codes for trees and most of them use struct to construct a tree node. Why is that?
What I'm doing is using classes for the nodes inside the tree. I'm having problems instantiating/comparing objects to NULL. How would I check if the object is NULL? I can see a fix if I used structs and have a pointer to the nodes instead.
Below is the code for the ".h" and .cpp" files
Code://bfTree.h //This is a best first search tree. #ifndef BFTREE_H #define BFTREE_H #include <iostream> #include <string> #include <vector> class bfTree { private: class tree_node { int visited; int result; double percentage; tree_node parent; std::vector<bfTree::tree_node> children; }; public: bfTree::tree_node root; bool is_empty(); }; #endifThe compiler errors:Code://bfTree.cpp #include <string> #include <vector> #include <iostream> #include "bfTree.h" bool bfTree::is_empty() { if (root == NULL) return true; else return false; }
I also realized that I might need to overload the "==" for object comparison. Is there any way to instantiate an object to NULL?Code:In file included from HexAI.h:9:0, from Game_of_hex.cpp:9: bfTree.h:17:15: error: field ‘parent’ has incomplete type Game_of_hex.cpp:60:5: warning: unused parameter ‘argc’ Game_of_hex.cpp:60:5: warning: unused parameter ‘argv’ In file included from HexAI.h:9:0, from HexAI.cpp:6: bfTree.h:17:15: error: field ‘parent’ has incomplete type In file included from bfTree.cpp:5:0: bfTree.h:17:15: error: field ‘parent’ has incomplete type bfTree.cpp: In member function ‘bool bfTree::is_empty()’: bfTree.cpp:9:15: error: no match for ‘operator==’ in ‘((bfTree*)this)->bfTree::root == 0l’ bfTree.cpp:13:1: warning: control reaches end of non-void function



LinkBack URL
About LinkBacks



