Hello, all!

I'm getting an error that I don't understand, and would greatly appreciate some other input from somebody more experienced than me!

So here's the problem:

I have a tree that I can put nodes on, take nodes off, search, etc etc.

I have one last function that I canot get to work.

I need it to: 1 - traverse my tree in an inorder fashion
2 - print out the contents of each node on the tree

The unique key for each node is the 7 digit phone number

I have a struct I called Data that consists of a string name and a string address.

Here is the snippet of my program that defines the functions of my tree:

template <class KeyType, class DataType> 
void BST<KeyType, DataType>::traverse(TraverseType traverseType, void (*visit)(DataType))
     switch (traverseType)
       case INFIX:
         _traverseInfix(root, visit);
	case PREFIX:
         _traversePrefix(root, visit);
       case POSTFIX:
         _traversePostfix(root, visit);

Here is the piece from my main program where I declare a function called printData:

template<class Data>
void printData(Data data)
  cout << "Address is: " << data.address << " name is: "
       << data.name << endl;

and finally here is the piece of code where I (attempt) to implement the traversal function in my main program.

result = tree.traverse(INFIX, printData(data));
      if (result)
      cout << "Traversal was: " << result << endl;
      cout << "Back to the old drawing board" << endl;

The error I get is:
Error: invalid use of void expression

Thank you in advance for your assistance.


8 Years
Discussion Span
Last Post by chococrack

At first glance, your traverse function is declared as a void, yet you are doing result = tree.traverse(INFIX, printData(data)); This is not valid. I don't know what result is, but if its a boolean, then you need to accordingly fix the definition of your traverse function.

Also if you are going to use a function pointer, then this is not correct tree.traverse(INFIX, printData(data)); It should be tree.traverse(INFIX, printData);

[B]void[/B] BST<KeyType, DataType>::traverse(TraverseType traverseType, void (*visit)(DataType))

Also, what the above comment suggests, your function is declared as a void (which means it doesn't return anything) and you're attempting to stuff the information returned (nothing) into the variable result.

Eliminate the result variable, or set traverse to a bool and have it return a true if a traversal takes place, false otherwise.


Ok I understand that. I changed it to your suggestion, and voila! Works perfectly.

I don't know why I didn't see it in the first place. I guess when you look at the almost 500 lines of code in a program you become blind to simple errors after a while :)

Thank you so much for your assisntance.



I have one other minor question about my code:

It gets the first 5 number/name/addresses from a .DAT file. The program extracts the info from the .DAT file and correctly places them on the tree, but it seems to duplicate the 5th (last) entry

The info in the .DAT file looks like this -

5551234:Jim:1 Computer Way
5551235:Ruth:55 Anywhere Drive


ifstream dfile;
dfile.open ("phone.dat");

while (dfile.good())
  std::getline(dfile, phone, ':');
  std::getline(dfile, name, ':');
  std::getline(dfile, address, '\n');
  key = atoi(phone.c_str());
  data.name = name;
  data.address = address;

  result = tree.insert (key, data);
  cout << "Result is: " << result << endl;

Can you explain why the last entry is duplicated?

Thank you



I found that is the last key it added to my tree had a value of 0.

I fixed(put a bandaid on?) the problem by adding

if (key == 0)

But I was wondering if there was a way to stop the program from adding the 6th element with key value 0 in the first place. Well let me rephrase that - I'm sure there is, but don't what it could be :)

Any suggestions?




I think whats happening is that on the last runthrough of your while loop, it is reading the last line, but not an EOF

Then its running through it again, getting squat for info?

Also dfile.good() only is checking for no errors, not end of file.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.