|
-
Jun 15th, 2006, 10:38 AM
#1
Thread Starter
Frenzied Member
[C++] Pointer to function giving error
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?
-
Jun 15th, 2006, 11:26 AM
#2
Re: [C++] Pointer to function giving error
I think you just need to add the 'address of' operator in front of Mammal:
Code:
pFunc = &Mammal::speak;
HTH!
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 15th, 2006, 03:03 PM
#3
Lively Member
Re: [C++] Pointer to function giving error
You cant use function pointers for member functions (non static) as you do with common functions. It needs a special declaration and you must involve an instance of the object to call them.
"bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)
-
Jun 15th, 2006, 03:07 PM
#4
Thread Starter
Frenzied Member
Re: [C++] Pointer to function giving error
 Originally Posted by bilm_ks
You cant use function pointers for member functions (non static) as you do with common functions. It needs a special declaration and you must involve an instance of the object to call them.
My book says you can 
I think you just need to add the 'address of' operator in front of Mammal:
That's it sunburt! I'm thinking certain compilers actually accept the form without the address of operator.
Thanks.
-
Jun 15th, 2006, 03:55 PM
#5
Re: [C++] Pointer to function giving error
 Originally Posted by bilm_ks
You cant use function pointers for member functions (non static) as you do with common functions. It needs a special declaration and you must involve an instance of the object to call them.
The code System_Error posted has both the special declaration and an instance which invokes the function -- so you're right, but uneccesary 
I think that when taking the address of a non-static member function, that the & is necessary. I know that with regular (non-class) functions, the & is optional.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Jun 16th, 2006, 08:22 AM
#6
Re: [C++] Pointer to function giving error
Correct. Basically, the form without the & was kept for C compatibility, but disliked, so when it came to member functions, where C compatibility was not an issue, so the & was made mandatory.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
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
|