-
August 9th, 2012, 01:39 AM
#1
[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??
-
August 9th, 2012, 02:08 AM
#2
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:isplay. However, the first part of that statement is incorrect.
-
August 9th, 2012, 02:30 AM
#3
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 "
-
August 9th, 2012, 02:34 AM
#4
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
-
August 9th, 2012, 02:54 AM
#5
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;
}
-
August 9th, 2012, 03:12 AM
#6
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
-
August 9th, 2012, 05:14 AM
#7
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.
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
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
August 9th, 2012, 03:31 AM
#8
Re: [RESOLVED] call to non-static member function without an object argument?
Aha, "Pointers to Member Functions". Than it makes sense, of course.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|