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);
         break;
	case PREFIX:
         _traversePrefix(root, visit);
         break;
       case POSTFIX:
         _traversePostfix(root, visit);
         break;
       }
 }

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;
      }
      else
      {
      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.

Jim

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.

Jim

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

etc

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

Jim

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)
{
tree.remove(key);
}

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?

Tyvm

Jim

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.

while(dfile)
{
    ...
}
This article has been dead for over six months. Start a new discussion instead.