Text Book Error Need Help!

This is a discussion on Text Book Error Need Help! within the C++ Programming forums, part of the General Programming Boards category; Hi, I have spent three hours copying and learning a large program in OOP, but when I compiled it, I ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Text Book Error Need Help!

    Hi, I have spent three hours copying and learning a large program in OOP, but when I compiled it, I got this error:

    Code:
    Documents\C++\Haunt\main.cpp request for member `health' in `play', which is of non-class type `Player ()(int, int, int)'
    I wont post all the code as it is very large, but here is the code that the class implelemnts.

    The error is referining to the line in main that reads:

    play.health++;

    code from header file:

    Code:
    /******* player class *********************************************************/
    
    class Player
    {
    public:
    	   int m_Health;
    	   int m_Magic;
    	   int m_Gold;
    	   
    	   Player( int health = 100, int magic = 1, int gold = 5 );	// constructor
    	   ~Player();	// deconstructor  
    	   
    private:
    		bool pdead;		// is player defeated ( at start of play )
    		bool palive;	// is player active ( at start of play )
    };
    code from class implemet file:

    Code:
    Player::Player( int health, int magic, int gold )
    {
         m_Health = health;
         m_Magic = magic;
         m_Gold = gold;
         
         palive = true;	// new game
         pdead = false;	// turns TRUE when player defeated
    }
    code from main:

    Code:
    void area1 ( void )
    {
     	 Player play( int health, int magic, int gold );
     	 
     	 play.health++;   // ERRPR LINE
    	 
     }
    It is the only compile error, and the book says on running, health should increase by one. I think the error is the line above the error line, but I am not sure. Please Help!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > play.health++
    The public variable in the class is called m_Heath
    So perhaps
    play.m_Health++;

    Public data in a class is poor OO in my opinion.

  3. #3
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    yep what Salem said should fix your prob.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    I did that but I am getting the same error. I have also looked online at a few other things and run debug mode on my compiler but it broke at that line saying an error must be corrected in order to continue.

    If I made the health private and not public and then incremented it within the function and not outside, would that fix the problem? I don't think I can think of a reason why a book would have faulty code

  5. #5
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    well from the code that you posted what Salem said is all we can probably deduce..
    Code:
    void area1 ( void )
    {
    	  Player play( int health, int magic, int gold );
    	  
    	  play.m_Health++;
    }
    You can make m_Health a private variable and create another function in the class to do the incrementing, but they way you have right now should work.. but if you want to try:

    Code:
    class Player
    {
      public:
    	int m_Magic;
    	int m_Gold;
    	   
    	Player( int health = 100, int magic = 1, int gold = 5 );	// constructor
    	~Player();	// deconstructor  
    	void incrementHealth();
    	   
       private:
    	 int m_Health;	
    	 bool pdead;		// is player defeated ( at start of play )
    	 bool palive;	// is player active ( at start of play 
    };
    
    void Player::incrementHealth()
    {
      m_Health++;
    }
    Code:
    Player play(...);
    play.incrementHealth();
    If you still can't get it to work you could always upload the source files in a .zip and I'm sure someone here would compile it and help pinpoint the error.
    Last edited by Kurisu; 02-18-2006 at 03:30 AM.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Ok thanks Kurisu, I will try the private member trick, and see what happens.. fingers crossed it might work!

  7. #7
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Code:
    void area1 ( void )
    {
     	 Player play( int health, int magic, int gold );
     	 
     	 play.m_Health++; 
    	 
     }
    Is this a correct way of declaring play? I would say make sure health, magic, and gold are all declared, but you declare them in the function call... For testing purposes leave out what's inside the parenthesis, since each has a default value.

    Also, why would you need two bools when one is sufficient? You have pdead and palive. If you have only one, say palive, and it is set to false, won't that also mean your player is dead on start?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    bool isZombie = isAlive && isDead;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about book
    By Kaidao in forum C++ Programming
    Replies: 6
    Last Post: 03-20-2006, 02:31 AM
  2. Text adventure engine idea. thoughts?
    By suzakugaiden in forum Game Programming
    Replies: 16
    Last Post: 01-15-2006, 04:13 AM
  3. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  4. Replies: 3
    Last Post: 05-25-2005, 01:50 PM
  5. Structure problem
    By mattz in forum C Programming
    Replies: 10
    Last Post: 11-30-2001, 12:19 PM

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