#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    11

    class constructor prob


    I can't see what the prob is here - the syntax error is at the line a=strength*health. Is it to do with the variables being public or not?

    #include <stdio.h>
    #include <stdlib.h>

    class Player
    {
    public:
    int health;
    int strength;
    int agility;

    Player();
    };

    Player::Player() {

    strength = 10;
    agility = 10;
    health = 10;
    }

    int main(int)
    {
    int a;
    a=strength*health;
    printf("%d",a);
    system("pause");
    }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    where is ur prefix of Class

    a = strength * health;

    Code:
    Player P;
    int a = P.strength * P.health;
    printf("%d\n", a);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    11
    Still isn't working, I get this error:

    23 C:\Program Files\c++\Dev-Cpp\ben-prog\class constructor test.cpp
    within

    at the line Player P.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    14
    Rep Power
    0
    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    class Player
    {
    	public:
    		int health;
    		int agility;
    		int strength;
    
    		Player();
    };
    
    Player::Player()
    {
    	health = 10;
    	agility = 10;
    	strength = 10;
    }
    
    int main()
    {
    	int a;
    	Player P;
    	a = P.health * P.strength;
    	printf("%d\n",a);
    	return 0;
    }
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    55
    Rep Power
    11
    Done - thanks!

IMN logo majestic logo threadwatch logo seochat tools logo