What the hell am I doing wrong?

Started by
4 comments, last by daerid 22 years, 1 month ago
I''m getting a weird error. Ok, I''m trying to do some c++ Win32 stuff. Here''s the important source:
  
class Window;

typedef bool (Window::*MsgDispatch)(unsigned int wparam,long lparam,long &retVal);

struct MsgHandler
{
  unsigned int _msg;
  MsgDispatch _pfn;
  MsgHandler(unsigned int msg = 0, MsgDispatch pfn = 0)
    : _msg(msg), _pfn(pfn) {}
};

class Window
{
// .. yadda yadda constructor, blah

public:
  void AddMsg(MsgHandler *mh)
  {
    _msghandlers.push_back(mh);
  }
protected:
  auto_vector<MsgHandler> _msghandlers; // auto vector is a class template that is basically an array of std::auto_ptr''s

};
  
Ok, so that compiles fine. Above is basically just a framework for handling windows messages. I get into trouble when I actually try to use the above code:
  
class MyWin : public Window
{
public:
  bool OnDestroy(unsigned int wparam,long lparam,long &retVal);
};

bool MyWin::OnDestroy(unsigned int wparam,long lparam,long &retVal)
{
  ::PostQuitMessage(0);
  retVal = 0;
  return true;
}

// Just stub code for now

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, PSTR szCmdLine, int iShowCmd)
{
  MyWin *win;
  // ... blah blah... create window here


  win->AddMsg(new MsgHandler(WM_DESTROY,MyWin::OnDestroy));

  return 0;
}

  
It LOOKS fine to me, but when I compile, I get this error msg:

error C2664: ''__thiscall MsgHandler::MsgHandler(unsigned int,bool (__thiscall Window::*)(unsigned int,long,long))'' : cannot convert parameter 2 from ''bool (__thiscall My*)(
unsigned int,long,long &)'' to ''bool (__thiscall Window::*)(unsigned int,long,long)''
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
 
Ok, so then I add a cast:
  
win->AddMsg(new MsgHandler(WM_DESTROY,(MsgDispatch)MyWin::OnDestroy));
  
And now I get this error:

error C2440: ''type cast'' : cannot convert from '''' to ''bool (__thiscall Window::*)(unsigned int,long,long)''
        None of the functions with this name in scope match the target type
 
What am I missing? Is this not the correct way of doing this kind of thing? This has been driving me nuts for a week. Help please! "Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
Advertisement
Oh, and I''m using Visual C++ 6.0, Service Pack 5

"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
quote:Original post by daerid
...

Ok, so then I add a cast:


win->AddMsg(new MsgHandler(WM_DESTROY,(MsgDispatch)MyWin::OnDestroy));
[/source]

And now I get this error:

error C2440: ''type cast'' : cannot convert from '' to ''bool (__thiscall Window::*)(unsigned int,long,long)''        None of the functions with this name in scope match the target type  


What am I missing? Is this not the correct way of doing this kind of thing?

This has been driving me nuts for a week. Help please!



I already tried that. :\

"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
One problem from what you posted with the cast is that you''re missing a closing parenthesis. If that''s not how it shows up in your code, the error sounds like a scope problem.

Sorry about the last reply, didn''t finish reading the post.
''sall right.

I double checked the parens, so that''s not the problem.

Hep!

"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com
GAH

WTF

Ok, found it.

My Typedef is expecting a long as the 3rd param instead of a long&

Thanks anyways guys.

"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool
daerid@gmail.com

This topic is closed to new replies.

Advertisement