Pointers to member functions using virtual methods

This is a discussion on Pointers to member functions using virtual methods within the C++ Programming forums, part of the General Programming Boards category; I'm working my way through SAMS Teach Yourself C++ in 21 Days Third Edition. But I have ran into a ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    England
    Posts
    2

    Question Pointers to member functions using virtual methods

    I'm working my way through SAMS Teach Yourself C++ in 21 Days Third Edition. But I have ran into a problem.

    When I try to compile this with Dev-C++ 4.9.9.2 I get an error message. I've tried compiling the code from the book and code on the CD, but both have the same result.

    Code:
       C:\Documents and Settings\Will\My Documents\My C++\Sams Teach Yourself\Week 2\Day 14\14.10\List1410.cpp In function `int main()': 
    67 C:\Documents and Settings\Will\My Documents\My C++\Sams Teach Yourself\Week 2\Day 14\14.10\List1410.cpp invalid use of non-static member function `virtual void Mammal::Speak() const' 
    67 C:\Documents and Settings\Will\My Documents\My C++\Sams Teach Yourself\Week 2\Day 14\14.10\List1410.cpp cannot convert `void (Mammal::)() const' to `void (Mammal::*)() const' in assignment 
    68 C:\Documents and Settings\Will\My Documents\My C++\Sams Teach Yourself\Week 2\Day 14\14.10\List1410.cpp invalid use of non-static member function `virtual void Mammal::Move() const' 
    68 C:\Documents and Settings\Will\My Documents\My C++\Sams Teach Yourself\Week 2\Day 14\14.10\List1410.cpp cannot convert `void (Mammal::)() const' to `void (Mammal::*)() const' in assignment
    Lines 67 and 68 are the following, right at the end of the program.

    Code:
    		case 1: pFunc = Mammal::Speak; break;
    		default: pFunc = Mammal::Move; break;
    Code:
     //Listing 14.10 Pointers to member functions using virtual methods
    
    #include <iostream>
    using namespace std;
    
    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;
    		default: pFunc = Mammal::Move; break;
    		}
    
    		(ptr->*pFunc)();
    		delete ptr;
    	}
    	return 0;
    }
    I would be thankful for any help.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You need to use the address operator (&):
    Code:
    case 1: pFunc = &Mammal::Speak; break;
    default: pFunc = &Mammal::Move; break;
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    May 2006
    Location
    England
    Posts
    2
    And it works! Thanks alot for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Static functions.... why?
    By patricio2626 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2007, 09:06 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. inheritance & virtual functions
    By xagiber in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2002, 12:10 PM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21