I have a very simple function pointer that I even got out of a book, but my compiler (dev cpp) is giving me the following errors:
35 C:\Dev-Cpp\main.cpp invalid use of non-static member function `virtual void Mammal::speak()'
35 C:\Dev-Cpp\main.cpp cannot convert `void (Mammal:
()' to `void (Mammal::*)()' in assignment
The code is as follows:
Code:
#include <iostream>
using namespace std;
class Mammal
{
public:
Mammal() { }
virtual ~Mammal() { }
Mammal(const Mammal&) { }
virtual void speak()
{
cout << "Mammal speak\n";
}
};
class Cat: public Mammal
{
public:
Cat() { }
~Cat() { }
Cat(const Cat&) { }
void speak()
{
cout << "Meow\n";
}
};
int main(int argc, char* argv[])
{
Mammal m;
void (Mammal::*pFunc)() = 0;
pFunc = Mammal::speak;
Mammal* ptr = new Cat;
(ptr->*pFunc)();
delete ptr;
}
What exactly am I doing wrong to recieve this error? Everything looks correct?