I’m running into syntax errors with C++ where I have to return a pointer to a function inline.
struct Note{}
Observer.hpp
class Observer {
protected:
void (*notify)(Note *note); // should this be *(*notify...)?
public:
void (*(*getNoteMethod)())(Note *note);
};
Observer.cpp
void (*Observer::getNoteMethod())(Note*) { //error: non-static data member defined out-of-line
return this->notify;
}
I’m getting this error, error: non-static data member defined out-of-line
I’m new to C++ and attempting to define the return function signature correctly.
>Solution :
Declare the member function as:
class Observer {
protected:
void (*notify)(Note *note);
public:
void (*getNoteMethod())(Note *note);
};
Better to declare the function pointer type in advance via using or typedef. E.g.
class Observer {
using function_pointer_type = void(*)(Note*);
protected:
function_pointer_type notify;
public:
function_pointer_type getNoteMethod();
};
Observer::function_pointer_type Observer::getNoteMethod() {
return this->notify;
}