int main(int argc, char *argv[])
{
int (*fnPtr)(int i);
foo f;
fnPtr = &foo::bar;
cout << (*fnPtr)(3);
system("PAUSE");
}
compiler output:
D:\Dev-Cpp\Untitled1.cpp [Warning] In function `int main(int, char **)':
15 D:\Dev-Cpp\Untitled1.cpp converting from `int (foo::*)(int)' to `int (*)(int)'
i copied this code from a tutorial, it SHOULD work, how can i disable the compiler warning, or get it to work?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
The wraning is there for a reason... you can't have a pointer to a member function without associating it with the class the member function is in. Whatever tutorial teaches you otherwise is wrong. The correct way to do it is as follows:
#include <iostream>
#include <stdlib.h>
class foo
{
public:
int bar(int i) { return i; }
};
int main(int argc, char *argv[])
{
int (foo::*fnPtr)(int i);
foo f;
fnPtr = &foo::bar;
cout << (f.*fnPtr)(3) << endl;
system("PAUSE");
return 0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
#include <iostream>
#include <cstdlib>
class foo
{
public:
int bar(int i){return i;}
};
int main(int argc, char *argv[])
{
int (*fnPtr)(int i);
foo f;
fnPtr = &foo::bar;
cout << (*fnPtr)(3);
system("PAUSE");
}
compiler output:
D:\Dev-Cpp\Untitled1.cpp [Warning] In function `int main(int, char **)':
15 D:\Dev-Cpp\Untitled1.cpp converting from `int (foo::*)(int)' to `int (*)(int)'
i copied this code from a tutorial, it SHOULD work, how can i disable the compiler warning, or get it to work?
Ah Figured it out.
The wraning is there for a reason... you can't have a pointer to a member function without associating it with the class the member function is in. Whatever tutorial teaches you otherwise is wrong. The correct way to do it is as follows:
#include <iostream>
#include <stdlib.h>
class foo
{
public:
int bar(int i) { return i; }
};
int main(int argc, char *argv[])
{
int (foo::*fnPtr)(int i);
foo f;
fnPtr = &foo::bar;
cout << (f.*fnPtr)(3) << endl;
system("PAUSE");
return 0;
}