开发者

problem passing listener class to a function

开发者 https://www.devze.com 2023-01-01 15:39 出处:网络
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:

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:

开发者_StackOverflow社区

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消