Instantiate private object of another class

Okay, so I have a fairly straight forward question. I am working on a project where I am making a binary search tree. The assignment requires that we use a class Dictionary to call in our main that we will read words into, and then pass them from Dictionary to the Btree. Basically the Btree object cannot be called within our main, it can only be called within our class Dictionary. So I figured the best way to approach this would be to create a private data variable of class Btree within Dictionary like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Dictionary{
        friend class Btree;
	public:
		Dictionary();
		~Dictionary();
		void add(string word, string definition);
		void remove(string word);
		void print();
		string getDefinition(string word);
	private:
                Btree bst;
		string word;
		string definition;


};


It is saying that class Btree is undefined though. What am I doing wrong? I figured that friending the class Btree would allow me to create a Btree object within Dictionary.

Thank you in advance for the help :)
The compiler is trying to tell you that it hasn't found a declaration of a class called btree. Friending the class is not the same as creating a class named btree.
I already have a class named btree in a btree.h and btree.cpp file. It is a complete class with a constructor. I am trying to create a variable for Btree that I can access through dictionary

Ideally, I'd like to creat this Btree object only after calling the constructor for class Dictionary, while still allowing the functions of Dictionary to access the instantiated Btree object

Maybe I'd have to create private pointer, and then when the constructor is called have the pointer reference the newly instantiated Btree object?
Last edited on
Okay so that seemed to work. I created a private pointer variable to the Btree object, and then inthe constructor created a new Btree object for it to reference. Thanks for the help. Marking as solved.
If Btree doesn't need to know about Dictionary then you could have just included the header for Btree in your Dictionary header. Using a pointer is the last ditch attempt - you should avoid it at all costs.

Please read:
http://www.cplusplus.com/articles/Gw6AC542/
Topic archived. No new replies allowed.