Reputation: 4073
//class.h
typedef double (*ffunct)(double x1, double y1, double x2, double y2);
class Class {
public:
static ffunct myfunct;
static void setFunct();
static double doSomething(double x1, double y1, double x2, double y2);
static void call();
}
//class.cpp
void Class::setFunct(){
Class::myfunct=Class::doSomething;
}
double Class::doSomething(double x1, double y1, double x2, double y2) {
cout << "Hello World" << endl;
}
void Class::call() {
Class::myfunct(1.0,2.0,3.0,4.0);
}
//main.cpp
…
Class::setFunct();
Class::call();
…
Running the programm results in Undefined symbols for architecture x86_64: "Class::myfunct", referenced from Class::setFunct, Class::call…
So what am I doing wrong?
Upvotes: 0
Views: 990
Reputation: 5699
add one line on the top of your .cpp file:
ffunct Class::myfunct=NULL;
Upvotes: 0
Reputation: 9837
static ffunct myfunct;
is a declaration
You need a definition of it also in the cpp file
ffunct Class::myfunct;
Upvotes: 1
Reputation: 96835
Your prototype of doSomething
has a return type of double
, but your implementation thereof has a return type of void
.
static double doSomething(...)
...
void Class::doSomething(...)
Fixing this won't clear all the errors however. You still have a few more as the other answers mention.
Upvotes: 1
Reputation: 308392
In your cpp file you need one more line:
ffunct Class::myfunct = NULL;
The class declaration said that the variable would exist somewhere but you never gave it a definition. Since it's not part of each object it has to be defined separately.
Upvotes: 2