Nested Class Accessing Classes Private Variables

This is a discussion on Nested Class Accessing Classes Private Variables within the C++ Programming forums, part of the General Programming Boards category; I have a Tree template - Code: template<typename T> class Tree { public: //node class //nodes should have value, their ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    Nested Class Accessing Classes Private Variables

    I have a Tree template -
    Code:
    template<typename T>
    class Tree {
    	
    public:
    	
    	//node class
    	//nodes should have value, their parent, and all of their children
    	//and functions to get all of those
    	class Node {
    		
    	public:
    		//when declaring a node, give it
    		//a value and a parent value
    		Node(T&, Node*&);
    		~Node();
    		
    		bool isRoot();
    		
    		T& getValue();
    		void setValue(T&);
    		
    		Node*& getParent();
    		std::vector<Node*>& getChildren();
    		int getNumOfChildren();
    		
    		std::string toString();
    		std::string childrenToString();
    		
    		bool operator==(const Node& other);
    	private:
    		T value;
    		Node* parent;
    		std::vector<Node*> children;
    	};
    	
    	Tree();
    	Tree(T&);
    	~Tree();
    	
    	Node*& getRoot();
    	
    	void add(T&, Node*&);
    	
    	
    private:
    
    	//root of tree
    	Node* dummyroot;
    	Node* root;
    };


    I want the isRoot() function to just be something like this -



    Code:
    template<typename T>
    bool Tree<T>::Node::isRoot() {return this == root;}

    When I do this, it tells me invalid use of nonstatic data member root. How can I access root without making it public? A couple random things I tried were-

    this == &root
    this == Tree<T>::root

    I don't want to give a Tree variable for each Node because I don't think it makes sense. I want the Nodes to just be a Node with a value, parent, and children. Any help would be appreciated.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,526
    Since you are using a root variable without a particular association to a tree, you would get that error, since root is not a static member. I don't think making root a static member is the answer.

    Incidentally, isn't this something you'd want to handle in Tree? A tree ought to know where it's root node is. I would just make this a method for tree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Currency Calculator
    By desiamerican in forum C# Programming
    Replies: 1
    Last Post: 01-21-2010, 08:53 PM
  2. accessing private class data low overhead
    By lawrenced in forum C++ Programming
    Replies: 23
    Last Post: 07-28-2009, 09:14 AM
  3. Replies: 10
    Last Post: 07-26-2008, 08:44 AM
  4. abstract class
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 01-01-2005, 08:08 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21