0

I have the following header:

class MyClass {
private:
    static void (*OnRequest)();
    static void (*OnReceive)(int numBytes);

public:
    MyClass();

    static void SetOnReceive(void (*function)(int));
    static void SetOnRequest(void (*function)(void));    

};

void NonClassFunction();

and the following declaration:

#include "MyClass.h"
MyClass::MyClass() {
...
}

void MyClass::SetOnReceive(void (*function)(int) ) {
    OnReceive = function;
}

void MyClass::SetOnRequest( void (*function)(void) ) {
    OnRequest = function;
}

void NonClassFunction() {
    MyClass::OnRequest();
}

The code compiles fine but I get the following errors when I link:

unresolved symbol MyClass::OnReceive, first referenced in ./src/MyClass.obj
unresolved symbol MyClass::OnRequest, first referenced in ./src/MyClass.obj

I need OnRequest and OnReceive to function like a callback through NonClassFunction(). The NonClassFunction is being called by an interrupt so there is a bit of object oriented mangling going on here. MyClass is designed to be inherited. Ideally I would like OnRequest and OnReceive to be virtual but you cannot make static methods virtual.

zam664
  • 757
  • 1
  • 7
  • 18

4 Answers4

1

Those are linker error, which means the members are not defined. They're only declared.

The pointer members are static members, so they need definition, which is outside the class.

Do this in the .cpp file:

void (*MyClass::OnRequest)();
void (*MyClass::OnReceive)(int);

These are definitions, and what you've written in the class are only declarations.

Note the position of * in the above definitions. A slight mistake such as these:

void (MyClass::*OnRequest)();    //notice the difference
void (MyClass::*OnReceive)(int); //notice the difference

would change the meaning completely! Now these are pointers-to-non-static-member-function. So know the difference and be careful. :-)

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Thanks. I tried something closer to what you said not to do void MyClass::(*OnRequest)(); and it would not compile. I tried what you said to do and it worked perfectly. Thanks again. – zam664 Apr 04 '13 at 03:50
1

These two variables in your header:

static void (*OnRequest)();
static void (*OnReceive)(int numBytes);

Have not been defined.

Define them in your cpp file.

void (*MyClass::OnRequest)();
void (*MyClass::OnReceive)(int);
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
1

You provided the declarations of the function pointers, but not the definition. Add this to a single cpp file:

void (*MyClass::OnRequest)();
void (*MyClass::OnReceive)(int);
David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489
0

i asked the same question recently how-to-send-a-message-to-the-class-that-created-the-object

enemies_array[0].enemy =  new Enemy(this,&Game::EnemyEvent);   

typedef void (Game::*ChangeFunc)(DWORD &)
Class Enemy
{
private:
   ChangeFunc iChange;
   Game *pGame;
}:

Enemy(Game *pCreatorGame, ChangeFunc iChangeHandler )
{
    iChange = iChangeHandler;
    pGame = pCreatorGame;
}

void Enemy::Draw(D3DGraphics& gfx)
{
    (pGame->*iChange)(this->dwThreadID);
Community
  • 1
  • 1
NaturalDemon
  • 934
  • 1
  • 9
  • 21