Thread: Ok I am trying to compile this but it won't

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Ok I am trying to compile this but it won't

    Code:
    #include <iostream>
    
    
    class Dog
    {
    public:
    	void Speak()const { cout << "Woof!\n"; }
    	void Move() const { cout << "Walking to heel...\n"; }
    	void Eat() const { cout << "Gobbling food...\n"; }
    	void Growl() const { cout << "Grrrrr\n"; }
    	void Whimper() const { cout << "Whining noises...\n"; }
    	void RollOver() const { cout << "Rolling over...\n"; }
    	void PlayDead() const { cout << "The end of Little Caesar?\n"; }
    };
    //problem seems to be around here
     void (Dog::*[])()const ;
    int main()
    {
    	const int MaxFuncs = 7;
      void	(Dog::* DogFunctions[MaxFuncs])() const =
    		{Dog::Speak,
    		 Dog::Move,
    		 Dog::Eat,
    		 Dog::Growl,
    		 Dog::Whimper,
    		 Dog::RollOver,
    		 Dog::PlayDead };
    
    	Dog* pDog =0;
    	int Method;
    	bool fQuit = false;
    
    	while (!fQuit)
    	{
    		cout << "(0)Quit (1)Speak (2)Move (3)Eat (4)Growl";
    		cout << " (5)Whimper (6)Roll Over (7)Play Dead: ";
    	cin >> Method;
    		if (Method == 0)
    		{
    			fQuit = true;
    		}
    		else
    		{
    			pDog = new Dog;
    			(pDog->*DogFunctions[Method-1])();
    			delete pDog;
    		}
    	}
    	return 0;
    }
    [
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Ok I got it never mind......

    Code:
    //Listing 15.11 Array of pointers to member functions
    
    #include <iostream>
    
    
    class Dog
    {
    public:
    	void Speak()const { cout << "Woof!\n"; }
    	void Move() const { cout << "Walking to heel...\n"; }
    	void Eat() const { cout << "Gobbling food...\n"; }
    	void Growl() const { cout << "Grrrrr\n"; }
    	void Whimper() const { cout << "Whining noises...\n"; }
    	void RollOver() const { cout << "Rolling over...\n"; }
    	void PlayDead() const { cout << "The end of Little Caesar?\n"; }
    };
    
     void (Dog::*DogFunctions)()const ;
    int main()
    {
     int MaxFuncs = 7;
    	(Dog:: * DogFunctions[MaxFuncs])()const =
    		{Dog::Speak,
    		 Dog::Move,
    		 Dog::Eat,
    		 Dog::Growl,
    		 Dog::Whimper,
    		 Dog::RollOver,
    		 Dog::PlayDead };
    
    	Dog* pDog =0;
    	int Method;
    	bool fQuit = false;
    
    	while (!fQuit)
    	{
    		cout << "(0)Quit (1)Speak (2)Move (3)Eat (4)Growl";
    		cout << " (5)Whimper (6)Roll Over (7)Play Dead: ";
    	cin >> Method;
    		if (Method == 0)
    		{
    			fQuit = true;
    		}
    		else
    		{
    			pDog = new Dog;
    			(pDog->*DogFunctions[Method-1])();
    			delete pDog;
    		}
    	}
    	return 0;
    }
    Compiles beautifully.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Ok first it compiles then it doesn't........gets on my nerves.

    [huge edit] I got it now..... [/huge edit]
    Last edited by incognito; 03-10-2002 at 01:18 AM.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Don't you just love function pointers?
    I will admit that function pointers are one of the more confusing aspects of C++, but they can be immensely useful once you get the syntax down.
    Code:
    #include <iostream>
    using namespace std;
    
    class Dog
    {
      public:
        void Speak(){ cout << "Woof!\n"; }
        void Move(){ cout << "Walking to heel...\n"; }
        void Eat(){ cout << "Gobbling food...\n"; }
        void Growl(){ cout << "Grrrrr\n"; }
        void Whimper(){ cout << "Whining noises...\n"; }
        void RollOver(){ cout << "Rolling over...\n"; }
        void PlayDead(){ cout << "The end of Little Caesar?\n"; }
    };
    
    void (Dog::*DogFunctions[])() = 
    {
      &Dog::Speak, &Dog::Move, &Dog::Eat, &Dog::Growl,
      &Dog::Whimper, &Dog::RollOver, &Dog::PlayDead
    };
    
    int main()
    {
      Dog dog;
      int Method;
      bool fQuit = false;
      while (!fQuit) {
        cout << "(0)Quit (1)Speak (2)Move (3)Eat (4)Growl";
        cout << " (5)Whimper (6)Roll Over (7)Play Dead: ";
        cin >> Method;
        if (Method == 0)
          fQuit = true;
        else
          (dog.*DogFunctions[Method-1])();
      }
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Hey thanks prelude that's even shorter than the other......one of the beauty of computer programming there's always an alternative to things.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM