Re: call to non-static member function without an object argument?
I believe it's supposed to be a pointer to that function declaration, I'm still learning so I could be mistaken.
The code came from an online course that I'm using to learn.
The code is kind of long so I'll show you a similar example with the same type of error :
Code:
#include <iostream.h>
enum BOOL {FALSE, TRUE};
class Mammal
{
public:
Mammal():itsAge(1) { }
virtual ~Mammal() { }
virtual void Speak() const = 0;
virtual void Move() const = 0;
protected:
int itsAge;
};
class Dog : public Mammal
{
public:
void Speak()const { cout << "Woof!\n"; }
void Move() const { cout << "Walking to heel...\n"; }
};
class Cat : public Mammal
{
public:
void Speak()const { cout << "Meow!\n"; }
void Move() const { cout << "slinking...\n"; }
};
class Horse : public Mammal
{
public:
void Speak()const { cout << "Winnie!\n"; }
void Move() const { cout << "Galloping...\n"; }
};
int main()
{
void (Mammal::*pFunc)() const =0;
Mammal* ptr =0;
int Animal;
int Method;
BOOL fQuit = FALSE;
while (fQuit == FALSE)
{
cout << "(0)Quit (1)dog (2)cat (3)horse: ";
cin >> Animal;
switch (Animal)
{
case 1: ptr = new Dog; break;
case 2: ptr = new Cat; break;
case 3: ptr = new Horse; break;
default: fQuit = TRUE; break;
}
if (fQuit)
break;
cout << "(1)Speak (2)Move: ";
cin >> Method;
switch (Method)
{
case 1: pFunc = Mammal::Speak; break; //ERROR HERE
default: pFunc = Mammal::Move; break; // ERROR HERE
}
(ptr->pFunc)();
delete ptr;
}
}
In Main(), in switch (method) I get the same error in case 1 and in default:
"call to non-static function without an object argument "
Re: [RESOLVED] call to non-static member function without an object argument?
yeah i caught that too, seems that things are missing in the example codes.
This chapter is about Special Classes and Functions : Pointers to Member Functions.
Re: [RESOLVED] call to non-static member function without an object argument?
Originally Posted by davidthecoder
This chapter is about Special Classes and Functions : Pointers to Member Functions.
Pointers to member functions is something you hardly ever use in modern C++. You should use function objects (std::function, std::tr1::function, boost::function) instead whenever possible, because they are much more flexible and the syntax is much more readable.
Bookmarks