Sign in to follow this  
monsterenergy

Help with class

Recommended Posts

Programming language = c++; compilor = visual c++ express; Alright so i am trying to make a class and use it to store information that a user is able to input at a command prompt. I have created the outline of the class. So I am trying to use the class in my main program I am going to post the header file for the class then the .cpp file for the class

//player1.h
class player1
{
public:
	char name;
	int health;
	player1(void);
public:
	~player1(void);
};

//player1.cpp
#include "player1.h"

player1::player1(void)
{
	name = temp;
	int health;
}
player1::~player1(void)
{
	name;
	health;
}

Now I am trying to use the class in my main file so I have some code that I am trying to have the user enter a name and store it in the class.
I have tried this so far
player1::name = joe; I get errors when trying to do this then i tried player1::player1 (joe,100); I get errors trying this I don't know i need some help damn newbie here get pwnd again




Share this post


Link to post
Share on other sites
1) Classes are instantiated into objects. So what you want to do is create a player. Well give him a random name... say brett.

Player brett;

then you can access his public method/data members like so

brett.health = 10;

2) You are redeclaring your health variable in the constructor of Player. Its already been declared

3) char refers to a single character not a a whole string. Also strings are written in quotes like this. brett.name = "hello";

...

Actually your code contains many many many problems. Its probably more worth it to read through a tutorial.

Share this post


Link to post
Share on other sites
ibebrett is right there are quite a few issues with the above code that need to be address and suggest a lack of understanding for basic syntax. I've commended you code below to hopefully give you some help;

//player1.h
class player1
{
public:
char name; //JD - This is a single char, you should use std::string
int health;
player1(void);
public: //JD - This is not incorrect but is not needed as the above one covers
//this.
~player1(void);
};

//player1.cpp
#include "player1.h"

player1::player1(void)
{
name = temp; //JD - This should not compile, what is 'temp'?
int health; //JD - This is a LOCAL variable declared in the constructor
//not initialising your member.
}
player1::~player1(void)
{
name; //JD - I'm not sure what these are here for, these lines are
health;//nothing.
}



Here I’ve re-written it below, if you read this and don't understand what I’m doing then i would recommend looking up some beginner C++ tutorials or maybe a good book Accelerated C++ is a great one;

//--------player1.h----------
#include <string>

class player1
{
public:
player1(void); //Constructor
~player1(void);//Destructor

private:
std::string name; //Player's name
int health;//Player's health

};

//---------player1.cpp------------
#include "player1.h"

player1::player1(void):name(""),health(0)
{
//Name and health both initialised using init list, but for example
name = "MyPlayer";
health = 100;
}
player1::~player1(void)
{
//Nothing in here, no need to de-allocate memory or anything like that
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this