Archived

This topic is now archived and is closed to further replies.

C++ Help (involves inheritence)

This topic is 5124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, I got bored and took a short break from Win32 programming and I set out to make an RPG battle system type program in console, but I'm having some problems. I have 2 classes which I'm having trouble with right now, the Player class and the Character class. The charater class will hold the player's hp and the enemie's hp for easy life deduction. They all have seperate files because I like things like that. Here's my code: character.h
#ifndef CHARACTER_H
#define CHARACTER_H

class Character
{
public:
	Character(){}

protected:
	int p_hp;
};


#endif

player.h
#ifndef PLAYER_H
#define PLAYER_H

#include <iostream>
#include <time.h>
#include "enemy.h"
#include "character.h"

class Player : public Character
{
public:
	Player();               // Default Constructor


private:
	char *   Pname;		// The players name

	int	 Killed;	// How many enemies a specific player has killed

};

#endif

player.cpp
#include "player.h"

Player::Player():
Killed(0),
Character::p_hp(20)
{
	char * name = new char;
	std::cout << "What's would you like to name your character? \t";
	std::cin >> name;
	Pname = name;
	std::cout << "You will from now on be known as " << name << "!\n";
	std::cout << name << " has killed " << Killed << " enemies!\n";
}
I have other files but right now these are the one's I'm concerned with. When I try to compile what I have, I get an error in "player.cpp" saying this: player.cpp(6) : error C2614: 'Player' : illegal member initialization: 'p_hp' is not a base or member Line 6 in that is where I initialize the needed variables for the created player, the line is this:
 Character:: P_hp(20)   
What am I doing wrong? (I had to put a space between the : and the P because of the smileys)
R.I.P. Mark Osback Solo Pa Mi Gente VG-Force [edited by - Ekim_Gram on November 30, 2003 3:26:20 PM] [edited by - Ekim_Gram on November 30, 2003 3:28:26 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I _think_ intializers can only be done on the durrent class, and don''t affect the parent classes.

the solution is to create a constructor in the parent class which initializes the hp member and to call that constructor with your hp value in the initializer list.

class P {
P(int x):m_x(x) {}
};
class D : P {
p(int y, int x):m_y(y),P(x) {}
};

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think hapaboy was refering to this line:

char * name = new char;

That creates space for one character. Also, from what we can see here, that space is never deleted.

And I think the other AP was right. Make a constructor for Character that looks like Character(int n), then use that in Player''s constructor:

Player:layer() : Killed(0), Character(20)
{
//body of the constructor goes here
}

Share this post


Link to post
Share on other sites