CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2012
    Posts
    4

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

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Aug 2012
    Posts
    4

    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 "

  4. #4
    Join Date
    Aug 2012
    Posts
    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

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: [RESOLVED] call to non-static member function without an object argument?

    pFunc is actually a pointer to function. However, this is wrong
    Code:
    (ptr->pFunc)();
    because pFunc is not a member of Mammal. It has to be:
    Code:
    (ptr->*pFunc)();
    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;
       }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Aug 2012
    Posts
    4

    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

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: [RESOLVED] call to non-static member function without an object argument?

    Aha, "Pointers to Member Functions". Than it makes sense, of course.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: [RESOLVED] call to non-static member function without an object argument?

    Quote Originally Posted by davidthecoder View Post
    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

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
  •  





Click Here to Expand Forum to Full Width

Featured