tags:

views:

42

answers:

1

I'm using a library that manipulates a binary search tree. In this library is a function that traverses the tree and passes each node it finds to a callback class:

bool TCODBsp::traverseInvertedLevelOrder(ITCODBspCallback *callback, void *userData)

ITCODBspCallback is a base class in the library from which the user is supposed to derive his own callback class to pass to the function. Here is the base class:

class ITCODBspCallback {
public :
    virtual bool visitNode(TCODBsp *node, void *userData) = 0;
};

Here's my derived class:

class MyCallback: public ITCODBspCallback
{
    public:
        virtual bool visitNode(TCODBsp*, void*); // defined in my implementation file
};

I then pass MyCallback to the function like this:

bsp->traverseInvertedLevelOrder(new MyCallback(), NULL);

and g++ gives me the following errors:

expected type-specifier before 'MyCallback'
expected ')' before 'MyCallback'
no matching function for call to 'TCODBsp::traverseInvertedLevelOrder(int*, NULL)'
note: candidates are: bool TCODBsp::traverseInvertedLevelOrder(ITCODBspCallback*, void*)

Anyone know what's wrong? I'm curious why it thinks MyCallback is an int*, in particular.

+2  A: 

This all looks like you forgot to include the MyCallback header. Since its parser doesn't interpret MyCallback as a type if it doesn't know it is one, it comes up with an own type, and ignores MyCallback(), i think. The type it comes up with is int*.

Notice that your code leaks because you need to call delete on any new'ed object. There is nothing wrong with creating objects like this:

MyCallback b;
bsp->traverseInvertedLevelOrder(&b, NULL);

In this case you are free'd of memory management.

Johannes Schaub - litb
My implementation file includes the header. Although, both `MyCallback` and the function in the implementation file that calls `traverseInvertedLevelOrder` are private members of the same class. Would this have something to do with it?
Max
@Max i think you should provide more code. Code like `struct A { void f() { new B; } struct B { }; };` is fine. So your bug is elsewhere.
Johannes Schaub - litb
Changing my function call to what you suggested, as well as moving `MyCallback` out of its class, fixed the problem. Not exactly sure why, but it was probably just a simple error on my part. Thanks for your help.
Max