Sunday 15 May 2011

xcode - C++ inheritance/undeclared identifier issue -



xcode - C++ inheritance/undeclared identifier issue -

i downloaded c++ files companion site 1 of textbooks. added files xcode, , wrote function test classes downloaded.

the 2 files classes binary tree , class binary search tree inherits binary tree class.

my problem 1 of members of super class isn't getting declared when subclass inherits super class. next examples i've seen , don't see issue. posting relevant code below error getting.

binarytree.h:

#include <iostream> using namespace std; //definition of node template <class elemtype> struct binarytreenode { elemtype info; binarytreenode<elemtype> *llink; binarytreenode<elemtype> *rlink; }; //definition of class template <class elemtype> class binarytreetype { public: const binarytreetype<elemtype>& operator= (const binarytreetype<elemtype>&); //overload assignment operator. bool isempty() const; //returns true if binary tree empty; //otherwise, returns false. void inordertraversal() const; //function inorder traversal of binary tree. void preordertraversal() const; //function preorder traversal of binary tree. void postordertraversal() const; //function postorder traversal of binary tree. int treeheight() const; //returns height of binary tree. int treenodecount() const; //returns number of nodes in binary tree. int treeleavescount() const; //returns number of leaves in binary tree. void destroytree(); //deallocates memory space occupied binary tree. //postcondition: root = null; binarytreetype(const binarytreetype<elemtype>& othertree); //copy constructor binarytreetype(); //default constructor ~binarytreetype(); //destructor protected: binarytreenode<elemtype> *root; private: void copytree(binarytreenode<elemtype>* &copiedtreeroot, binarytreenode<elemtype>* othertreeroot); //makes re-create of binary tree //othertreeroot points. pointer copiedtreeroot //points root of copied binary tree. void destroy(binarytreenode<elemtype>* &p); //function destroy binary tree p points. //postcondition: p = null void inorder(binarytreenode<elemtype> *p) const; //function inorder traversal of binary //tree p points. void preorder(binarytreenode<elemtype> *p) const; //function preorder traversal of binary //tree p points. void postorder(binarytreenode<elemtype> *p) const; //function postorder traversal of binary //tree p points. int height(binarytreenode<elemtype> *p) const; //function homecoming height of binary tree //to p points. int max(int x, int y) const; //returns larger of x , y. int nodecount(binarytreenode<elemtype> *p) const; //function homecoming number of nodes in binary //tree p points int leavescount(binarytreenode<elemtype> *p) const; //function homecoming number of leaves in binary //tree p points };

binarysearchtree.h:

#include "binarytree.h" #include <iostream> #include <cassert> using namespace std; template <class elemtype> class bsearchtreetype: public binarytreetype<elemtype> { public: bool search(const elemtype& searchitem) const; //function determine if searchitem in binary //search tree. //postcondition: returns true if searchitem found in // binary search tree; otherwise, returns false. void insert(const elemtype& insertitem); //function insert insertitem in binary search tree. //postcondition: if no node in binary search tree has // same info insertitem, node info insertitem // created , inserted in binary search tree. void deletenode(const elemtype& deleteitem); //function delete deleteitem binary search tree //postcondition: if node same info deleteitem // found, deleted binary search tree. private: void deletefromtree(binarytreenode<elemtype>* &p); //function delete node p points deleted //from binary search tree. //postcondition: node p points deleted // binary search tree.

and next code using test classes:

#include <iostream> #include "binarysearchtree.h" using namespace std; void testbinarysearchtree(); int main() { testbinarysearchtree(); homecoming 0; } void testbinarysearchtree() { bsearchtreetype<int> mytree; mytree.insert(50); mytree.insert(40); mytree.insert(30); mytree.insert(60); mytree.insert(70); mytree.insert(45); mytree.insert(65); mytree.insert(55); }

the error getting fellow member variable root of binarytree superclass not beingness declared whenever object of bsearchtreetype created.

root dependent name , requires special consideration either bring scope or forcefulness lookup. didn't include code accesses variable, code terrible plenty have doubts author got right.

please allow know book btw can set on list of books avoid. naming conventions lone create me want puke. there glaring technical flaws though such public inheritance of type lacking protected or virtual destructor...and no reason.

to access root correctly within scope of bsearchtreetype<elemtype> need utilize binarytreetype<elemtype>::root.

edit: think more correctly stated, root want dependent name , unless forcefulness utilize dependent name lookup won't. means looks root outside of dependent scopes before template instantiation , doesn't find root you're expecting to. author used compiler doesn't utilize two-stage lookup such msvc++...if bothered compile illustration code @ all. compilers supposed two-stage lookup. if there non-dependent root name somewhere in scope right compiler utilize instead of base of operations class's version.

c++ xcode class inheritance

No comments:

Post a Comment