c++ - Can't access object variables passed in a std callback -


i have callback function (ouside class) i'm passing bassmusicplayer class object parameter. if breakpoint inside it, can see variables , methods in 'self'.

if try read/print values, won't compile , give error c2027: use of undefined type 'bassmusicplayer'. added static class definition suggested others. use global variables workaround want avoid doing that.

this have in class file:

class bassmusicplayer; void __stdcall loopsyncproc(hsync handle, dword channel, dword data, void *user) {     bassmusicplayer * self = reinterpret_cast<bassmusicplayer*>(user);     //printf("%d\n", self->loop_start);     //if (!bass_channelsetposition(channel, self->loop_start, bass_pos_byte))     if (!bass_channelsetposition(channel, 0, bass_pos_byte)) // try seeking loop start         bass_channelsetposition(channel, 0, bass_pos_byte); // failed, go start of file instead }    class bassmusicplayer { private:     std::string          m_filename;     int                  m_filetype;     int                  m_music_handle;     bassmusicplayer*     m_music;     hwnd                 m_handle;     dword                m_sample_rate;     syncproc*            m_callback_proc;  public:     qword loop_start, loop_end;      // constructor     bassmusicplayer(hwnd handle, dword sample_rate, syncproc* callback_proc) {         m_handle = handle;         m_sample_rate = sample_rate;         m_callback_proc = loopsyncproc;     }      bool openmusicfile(std::string filename, qword seek_start, qword file_length, bool start_playing, bool loop, qword loopstart, qword loopend) {         loop_start = loopstart;         loop_end = loopend;         m_music_handle = bass_musicload(false, filename.c_str(), 0, 0, bass_music_posreset, m_sample_rate);         bass_channelsetsync(m_music_handle, bass_sync_end | bass_sync_mixtime, 0, m_callback_proc, this);     } }; 

why bassmusicplayer class not being recognized when want access variables?

your bassmusicplayer cannot recognized because have this:

class bassmusicplayer; 

this called forward declaration. tells compiler class bassmusicplayer exists, doesn't tell class is, how space objects require, or members has. because pointers integers , doesn't matter point until try them, compiler can use pointers fine, moment attempt dereference 1 of them (such accessing functions or variables -> operator), compiler fails because doesn't know how this. doesn't find out until class declared later, after callback.

to fix this, move callback after declaration of class. because have know function in class, should forward declare function instead, this:

void __stdcall loopsyncproc(hsync handle, dword channel, dword data, void *user); 

the overall order of code should be:

  • forward declare loopsyncproc function
  • declare class
  • define loopsyncproc function

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -