J
Jakob Vesterstrom
Hi all
I have question regarding function passing.
I have class A
class A {
public:
...
void attach(double f(double in)) {
this->f = f;
}
double (*f) (double in);
};
with a method 'attach', which takes a function and attaches it to the
class member f. It is quite simple to attach the function 'afunc'
double afunc(double x) {
return x*x;
}
to A::f by the statements
A a;
a.attach(afunc);
However, if I declare a new class with a function member also called 'afunc'
class B {
public:
...
double afunc(double x) {
return x+x1;
}
};
and tries to do the attachment in the same manner
A a;
a.attach(b.afunc);
then I get this error from the compiler:
test.c: In function `int main()':
test.c:33: error: no matching function for call to `A::attach(<unknown type>)'
test.c:8: error: candidates are: void A::attach(double (*)(double))
My question(s) is(are):
Why is the type of b.afunc unknown to the compiler?
What is the best generic fix to this problem?
Regards,
Jakob Vesterstrom
I have question regarding function passing.
I have class A
class A {
public:
...
void attach(double f(double in)) {
this->f = f;
}
double (*f) (double in);
};
with a method 'attach', which takes a function and attaches it to the
class member f. It is quite simple to attach the function 'afunc'
double afunc(double x) {
return x*x;
}
to A::f by the statements
A a;
a.attach(afunc);
However, if I declare a new class with a function member also called 'afunc'
class B {
public:
...
double afunc(double x) {
return x+x1;
}
};
and tries to do the attachment in the same manner
A a;
a.attach(b.afunc);
then I get this error from the compiler:
test.c: In function `int main()':
test.c:33: error: no matching function for call to `A::attach(<unknown type>)'
test.c:8: error: candidates are: void A::attach(double (*)(double))
My question(s) is(are):
Why is the type of b.afunc unknown to the compiler?
What is the best generic fix to this problem?
Regards,
Jakob Vesterstrom