Results 1 to 6 of 6

Thread: [C++] Pointer to function giving error

  1. #1

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    [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?

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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.

  3. #3
    Lively Member
    Join Date
    Nov 2005
    Posts
    68

    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...)

  4. #4

    Thread Starter
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: [C++] Pointer to function giving error

    Quote 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.

  5. #5
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: [C++] Pointer to function giving error

    Quote 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.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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
  •  



Click Here to Expand Forum to Full Width