"Expression must have class type" error with Binary Tree class

Here is my code sample for my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	string word;
	BinaryTree Address();
	ifstream file;

	file.open("Lincoln.txt");
	
	
	if (file.fail() )
	{
		cout << "COULD NOT OPEN FILE";
	}

	while (file)
	{
		file >> word;
		Address.insert(word, Address.root); //error appears here
	}


}


the compiler says that I need to put a class type for Address within the while loop, but Address is declared above.
Last edited on
Try just
BinaryTree Address;
instead of
BinaryTree Address();.
Otherwise, the compiler will think that you're declaring a function.
Then an error occurs at the declaration saying, "no default constructor exists for class "BinaryTree"

Here is the class and constructor for my BinaryTree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class BinaryTree
{
public:
	class Node
	{
	public:
		ElementType data;
		Node *left;
		Node *right;
		int count;
		Node(ElementType new_data = NULL)
		{
			data = new_data;
			left = NULL;
			right = NULL;
			count = 0;
		};
	};
	Node *root;


	BinaryTree(ElementType new_data) { root = new Node(new_data); };
Last edited on
So then...define a default constructor?

Or construct Address using the constructor you've written.
Is that not what I was doing before hand? Using the one I've written, that is.
No, to use the constructor you've written, you'd have to do
BinaryTree Address(some_kind_of_data);.
is there a way that I can edit the program so that I wouldn't have to pass anything at the start?

I tried passing NULL beforehand but that brought up many other errors.
Last edited on
You could define a default constructor.

Normally, when you don't have any other constructors defined, the compiler makes a default one for you automatically, but since you have another constructor, you'll have to define the default constructor yourself:
1
2
3
BinaryTree() { /* ... */}
// Alternatively, you could just tweak your existing constructor:
BinaryTree(ElementType new_data = /* some default value */) { root = new Node(new_data); }

When I run the program like that it gives a runtime error about an invalid null pointer. The original constructor has the default data to be NULL.
There's probably something wrong with your insert function, then.

Probably related to not checking if root == NULL or something of that sort.
This is the insert function
1
2
3
4
5
6
7
8
9
10
11
void BinaryTree::insert(ElementType new_data, Node *&root)
{
	if (root->data == new_data)
		root->count++;
	else if (root == NULL)
		root = new Node(new_data);
	else if (new_data < root->data)
		insert (new_data, root->left);
	else if (new_data > root->data)
		insert (new_data, root->right);
}


if the root is NULL then new_data gets put into it
But you check if root == NULL only after trying to access root->data in your first if statement.
I switched the two even though that shouldn't make a difference. Error still persists.

The problem I'm having is that I need to pass an initial piece of data to my constructor for it to run properly, but I want to leave it empty from the start and put things in later. The insert function isn't the issue here. In the constructor, new_data is given an initial value of NULL, but that isn't working.
Okay, hang on, what type is ElementType?

If it's std::string (which I suspect it is), then in your Node default constructor, you try to construct data with NULL, which doesn't work.
(You should get the same error from just doing std::string str(NULL);.)

In general, I'd avoid setting anything that's not a pointer to NULL.

Try changing
Node(ElementType new_data = NULL)
to
Node(ElementType new_data = ElementType())
and see if that helps.

(If your BinaryTree default constructor also tries to set new_data to NULL, change that as well.)
Last edited on
that didn't change anything. and ElementType is a string
Last edited on
Really?
I copied/pasted your code and made the changes, and it works for me.

To clarify:
11
12
13
14
15
16
17
18
//Node(ElementType new_data = NULL)
Node(ElementType new_data = ElementType())
{
    data = new_data;
    left = NULL;
    right = NULL;
    count = 0;
};
22
23
24
BinaryTree(ElementType new_data = ElementType()) { root = new Node(new_data); };
// Or you could have something like this added instead:
BinaryTree() { root = new Node(); }
1
2
3
4
5
6
void BinaryTree::insert(ElementType new_data, Node *&root)
{
    if (root == NULL) // Swapped this with the next one
        root = new Node(new_data);
    else if (root->data == new_data)
        root->count++;
the error is in the main function here.

1
2
3
4
5
while (file)
	{
		file >> word;
		Address.insert(word, Address.root); //error appears here
	}
Well, it works on my end, so I don't know what's going on on your end.

Take a debugger and step through your insert function and see exactly where it goes wrong.
Topic archived. No new replies allowed.