[RESOLVED] call to non-static member function without an object argument?
Hi, I'm trying to learn c++. I just came across a error I couldn't figure out.
I keep getting this error: "call to non-static member function without an object argument "
For this line :
Code:
void (Part::*Func)()const = Part::Display();
its declared like this:
Code:
virtual void Display() const =0;
any idea how I could fix this??
Re: call to non-static member function without an object argument?
What is this suppose to do? It looks like a function declaration.
Code:
void (Part::*Func)()const = Part::Display();
Do you want to take the address of the Display function? Then you should use &Part::Display. However, the first part of that statement is incorrect.
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: call to non-static member function without an object argument?
but any how I did as you suggested and added the "&" reference operator and it came out working :) thanks :D
Re: [RESOLVED] call to non-static member function without an object argument?
pFunc is actually a pointer to function. However, this is wrong
because pFunc is not a member of Mammal. It has to be:
What is that chapter about? Because I think your example can be rather put like this:
Code:
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: ptr->Speak(); break;
default: ptr->Move(); break;
}
delete ptr;
}
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.
Your version is simpler :)
Re: [RESOLVED] call to non-static member function without an object argument?
Aha, "Pointers to Member Functions". Than it makes sense, of course. :)
Re: [RESOLVED] call to non-static member function without an object argument?
Quote:
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.
If you really do want to learn about pointers to member functions, here is a good reference: http://www.parashift.com/c++-faq/poi...o-members.html