Geo
Geo

Reputation: 96767

error C2436 member function or nested class in constructor initializer list

I have a function pointer declared in my header, like this:

typedef void (intCB)(int );

and I have a class member declared like this:

private:
  intCB m_intCB;

In my constructor's initialization list I want to initialize it with 0:

MyClass::MyClass : m_intCB(0)
{
   #ifdef SOMETHING
   m_intCB = &someOtherFunc;
   #endif
}

Only if a specific define is in place, I want to set m_intCB to it, if not I want to keep it on 0. The problem with the above code is that I receive:

error C2436: 'm_intCB' : member function or nested class in constructor initializer list

How can I fix it?

Upvotes: 2

Views: 3050

Answers (5)

Alok Save
Alok Save

Reputation: 206508

functioin pointer typedef Should be:

typedef void (*intCB)(int); //You missed the *

Upvotes: 1

user2100815
user2100815

Reputation:

And:

MyClass::MyClass : m_intCB(0)

should be:

MyClass::MyClass() : m_intCB(0)

Upvotes: 1

Eric Z
Eric Z

Reputation: 14505

Function pointer is a pointer, so don't miss *

typedef void (*intCB)(int);

Upvotes: 2

Asha
Asha

Reputation: 11232

Your typedef is wrong, it should be typedef void (*intCB)(int );

Upvotes: 2

CB Bailey
CB Bailey

Reputation: 791421

That's not a function pointer, you are missing a *. Try:

typedef void (*intCB)(int);

Upvotes: 8

Related Questions