2

i have a little problem using BASS library in c++. Playing works fine, but I want to jump to the next file, after the current is played until the end. For this kind of need BASS provides callbacks and I am using it like so (init already done)...

Foo.h:

class Foo
{
    public:
        Foo(void);
        ~Foo(void);
        void endOfFile(void);
    private:
        HSTREAM _streamHandle;
        void playFile(string);
};

Foo.cpp:

void Foo::playFile(string fileName)
{
    _streamHandle = BASS_StreamCreateFile(false, fileName.c_str(), 0, 0, BASS_STREAM_AUTOFREE);
    BASS_ChannelSetSync(_streamHandle, BASS_SYNC_END, 0, endOfFileCallback, this);
    BASS_ChannelPlay(_streamHandle, true);

void Foo::endOfFile()
{
    playFile(getNextFileFromSomewhere()); // obviously this is done different in production code
}

void CALLBACK endOfFileCallback(HSYNC handle, DWORD channel, DWORD data, void* pTarget)
{
    Foo* pFoo = static_cast<Foo*>(pTarget);
    pFoo->endOfFile();
}

So, this works, but it feels ugly, having a function and not a method called as callback and oppose endOfFile as a public method. It should be private. So I tried to use a method as callback...

Bar.h:

class Bar
{
    public:
        Bar(void);
        ~Bar(void);
    private:
        HSTREAM _streamHandle;
        void playFile(string);
        void endOfFile(void);
        void CALLBACK endOfFileCallback(HSYNC, DWORD, DWORD, void*); // now declaration in class
};

Bar.cpp:

void Bar::playFile(string fileName)
{
    _streamHandle = BASS_StreamCreateFile(false, fileName.c_str(), 0, 0, BASS_STREAM_AUTOFREE);
    BASS_ChannelSetSync(_streamHandle, BASS_SYNC_END, 0, endOfFileCallback, 0); // no reference to 'this' needed
    BASS_ChannelPlay(_streamHandle, true);

void Bar::endOfFile()
{
    playFile(getNextFileFromSomewhere()); // obviously this is done different in production code
}

void CALLBACK Bar::endOfFileCallback(HSYNC handle, DWORD channel, DWORD data, void* pTarget)
{
    endOfFile();
}

But this doesn't compile :-(

error: cannot convert 
    ‘Bar::endOfFileCallback’ 
from type 
    ‘void (Bar::)(HSYNC, DWORD, DWORD, void*) {aka void (**Player**::)(unsigned int, unsigned int, unsigned int, void*)}’ 
to type 
    ‘void (*)(HSYNC, DWORD, DWORD, void*) {aka void (*)(unsigned int, unsigned int, unsigned int, void*)}’

I guess you see the difference (Bar::) instead of (*). So the issue is clear, but sadly I am not skilled enough to solve it. I am doing c++ only in privat time and I am not so deep into callbacks, types and scopes. Can you people help me to find a working solution without public methods?

Thanks in advance!

Fatal

1 Answers1

0

I had the same error. I am also new to C++.

There is a big difference in C++ between pointers to functions and pointers to member functions. These answers helped me a lot:

Passing a member function as an argument to a constructor

C++ passing member function as argument

How can I pass a class member function as a callback

So either use the member function with the help of the listed answers, use static member functions or just use plain old functions that dont belog to a class.

Community
  • 1
  • 1
vuko_zrno
  • 635
  • 1
  • 8
  • 18