Thread: Won't compile, can YOU compile it on your compiler?

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

    Won't compile, can YOU compile it on your compiler?

    // Listing 15.7
    //demonstrates use of an array of pointers to functions

    #include <iostream>
    using namespace std;

    void Square (int&,int&);
    void Cube (int&, int&);
    void Swap (int&, int &);
    void GetVals(int&, int&);
    void PrintVals(int, int);

    int main()
    {
    int valOne=1, valTwo=2;
    int choice, i;
    const MaxArray = 5;
    void (*pFuncArray[MaxArray])(int&, int&);

    for (i=0;i<MaxArray;i++)
    {
    cout << "(1)Change Values (2)Square (3)Cube (4)Swap: ";
    cin >> choice;
    switch (choice)
    {
    case 1: pFuncArray[i] = GetVals; break;
    case 2: pFuncArray[i] = Square; break;
    case 3: pFuncArray[i] = Cube; break;
    case 4: pFuncArray[i] = Swap; break;
    defaultFuncArray[i] = 0;
    }
    }

    for (i=0;i<MaxArray; i++)
    {
    if ( pFuncArray[i] == 0 )
    continue;
    pFuncArray[i](valOne,valTwo);
    PrintVals(valOne,valTwo);
    }
    return 0;
    }

    void PrintVals(int x, int y)
    {
    cout << "x: " << x << " y: " << y << endl;
    }

    void Square (int & rX, int & rY)
    {
    rX *= rX;
    rY *= rY;
    }

    void Cube (int & rX, int & rY)
    {
    int tmp;

    tmp = rX;
    rX *= rX;
    rX = rX * tmp;

    tmp = rY;
    rY *= rY;
    rY = rY * tmp;
    }

    void Swap(int & rX, int & rY)
    {
    int temp;
    temp = rX;
    rX = rY;
    rY = temp;
    }

    void GetVals (int & rValOne, int & rValTwo)
    {
    cout << "New value for ValOne: ";
    cin >> rValOne;
    cout << "New value for ValTwo: ";
    cin >> rValTwo;
    }

    Ok it's been 3 consecutives example from the book that won't compile.....this is an example from STYC++ in 21 Days......Does it compile on your compiler.....by the way I see the problem in it I just want to know.const MaxArray = 5; <-- no return value declared......
    Last edited by incognito; 03-09-2002 at 09:39 PM.
    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
    I'll just include the file it doensn't show up good because of the vcode tags or something.
    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
    Sorry I am having problems with attaching this thing here it goes......hopefully.
    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
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Might just be a typo, especially if you are reading from an online book.

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Did it compile on yours? I am using STYC++ in 21 Days and I downloaded the source code.......if it works for you tell me what compiler you're using.
    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.

  6. #6
    Registered User blackwyvern's Avatar
    Join Date
    Jan 2002
    Posts
    60
    I think I may have found the culprit:


    switch (choice)
    {
    case 1: pFuncArray = GetVals; break;
    case 2: pFuncArray[i] = Square; break;
    case 3: pFuncArray[i] = Cube; break;
    case 4: pFuncArray[i] = Swap; break;
    defaultFuncArray[i] = 0;
    }
    }

    LOL, just joking with you

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Doesn't compile.

  8. #8
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    What compiler are you using?
    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.

  9. #9
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Dev-C++ (actually mingw for the technical people)

  10. #10
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Well I just tried this......
    Code:
    //Listing 15.10 Pointers to member functions using virtual methods
    
    #include <iostream.h>
    
    
    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;
    }
    and it wouldn't compile on DevC++ but once I tried it on Borland it worked.
    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.

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    --------------------------------------------------------------------------
    Editted because I didn't read thoroughly enough.
    --------------------------------------------------------------------------
    Last edited by golfinguy4; 03-10-2002 at 12:01 PM.

  12. #12
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Some compilers require you to be explicit when using the address of a function. This works on Dev C++ -

    Code:
    #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;
    }

  13. #13
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Oh I see, cool.....thanks for the help.
    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. Using DXUT with Borland 5.5 free compiler
    By Jedi Nescioquis in forum Game Programming
    Replies: 7
    Last Post: 02-11-2009, 12:04 PM
  2. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  3. Can't compile on the other compiler
    By AProg in forum C Programming
    Replies: 8
    Last Post: 05-25-2003, 07:55 AM
  4. My Dev compiler won't compile
    By Unregistered in forum C++ Programming
    Replies: 22
    Last Post: 05-04-2002, 06:37 PM
  5. Special Compiler for win app's
    By Unregistered in forum Windows Programming
    Replies: 19
    Last Post: 04-26-2002, 03:52 PM