Complete scrub: Issues with Class field with Pointer

I am doing a project for school and no I am not trying to cheat. I am genuinely trying to learn programming and have been sitting here going insane for over 8 hours already over something I know must be completely obvious.

I am trying to create a base class Player with several data fields, one being a pointer to an object of type Weapon - the class which derives from Player

My main issue I am assuming is how I've gone about with my Player constructor.
If not, then I'll need to review the rest of my code...

Player::Player( string pn, int ty, int lev, int str,
int wty, int wdur, int wlevReq)
: weapon1(wty, wdur, wlevReq)
{
setPlayerName(pn);
setType(ty);
setLevel(lev);
setStrength(str);
weapon1 = new Weapon(wty, wdur, wlevReq)
}

Remembering weapon1 is a pointer to an object, what's wrong here?? Also, I honestly just do not know where to declare the object weapon1 is pointing to. I was hoping it'd be taken care of with " weapon1 = new Weapon( . . . " in the Player constructor.
Why does Weapon derive from Player? Most likely you are getting infinite recursion because the player constructor constructs a weapon, which has to construct a player, and so on.
Last edited on



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Player
{
	public:

	//  Constructor ***********************************
	//  Uses default parameters or arguments passed
	//  Default arguments =		"No Name"-pn, 1-ty, 1-lev, 25-str, 
	//							1-wty, 25-wdur, 1-wlevReq
	//  Precondition - None
	//  Postcondition - object is initialized with valid values
		Player(string pn = "No Name", int ty = 1, int lev = 1, int str = 25, 
				int wty = 1, int wdur = 25, int wlevReq = 1);
	
	// Copy Constructor ******************************
	// Precondition - None
	// Postcondition - None
		Player(const Player& source);

	// Overloaded Assignment *************************
	// Precondition - None
	// Postcondition - None
		Player& operator=(const Player& source);


	//  Destructor ***********************************
	//  Precondition - None
	//  Postcondition - None
		~Player();

	//	Mutator Functions ***************************
        // . . .
	//	Accessor Functions **************************
        // . . .

	private:
		Weapon * weapon1;		// default = 1, 25, 1
		string playerName;		// default = "No Name"
		int type;				// default = 1 (1, 2, 3, 4)
		int level;				// default = 1
		int strength;			// default = 25
};


class Weapon
{
	public:

	//  Constructor **************************************
	//  Uses default parameters or arguments passed
	//  Default arguments = 1-ty, 25-dur, 1-levReq
	//  Precondition - None
	//  Postcondition - object is initialized with valid values
		Weapon(int ty, int dur, int levReq)
		{
			setType(ty);
			setDurability(dur);
			setLevelRequired(levReq);

	//Destructor ***************************************
	//  Precondition - None
	//  Postcondition - None
		~Weapon();
	
	//	Mutator Functions *******************************
        // . . . 
	//	Accessor Functions ******************************
        // . . . 

	private:

		int type;					// valid values = 1, 2, 3, 4
		int durability;				// default = 25
		int levelRequired;			// default = 1 (>= 1 && <= 100)
};





Note: this code is in both header and implementation files, I just merged them real quick to show you the code
Last edited on
It looks like your weapon class is non-polymorphic, so why are you using pointers at all?
Because I was told to. Don't really have an option on that.

How would I get my weapon class to be polymorphic and to actually work?
Do you know what polymorphism is? Do you even have to use it here?
I believe the assignment is going to be building up as the semester progresses. As of right now I just need to use a pointer to a Weapon object instead of an actual object.

The rest of the assignment is a game simulation that I haven't done a lick of coding forbecause I'm having so many issues with this!
I thought Weapon derives from player ?

if i were to make the constuctor for Player, i would do it :

1
2
3
4
Player ( string pn = "No Name", int ty = 1, int lev = 1, int str = 25, 
               int wty = 1, int wdur = 25, int wlevReq = 1)
    : playerName(pn), type(ty), level(lev), strength(str), weapon1(new Weapon(wty, wdur, wlevReq))
{ }
Last edited on
It makes no sense for Weapon to derive from Player.

Back to the original post, I have to ask, what is wrong here? I certainly can't tell what's wrong, so why do you think there is something wrong?
Last edited on
I'm getting 28 different errors, that's how I know lmao. All surrounding my pointer to the Weapon object.


Number 1 error is:

Error 1 error C2143: syntax error : missing ';' before '*'

they tell me the error lies here:
1
2
3
4
5
6
7
private:

		Weapon *pWeapon;		// default = 1, 25, 1
		string playerName;		// default = "No Name"
		int type;				// default = 1 (1, 2, 3, 4)
		int level;				// default = 1
		int strength;			// default = 25 


*EDIT*
for that same line of code they also give me the error

Error 2 error C4430: missing type specifier - int assumed. Note: C++
Last edited on
It looks like your weapon class is non-polymorphic, so why are you using pointers at all?

There is no inheritance here. Classes don't have to be polymorphic to use pointers.

Edit: I say there is no inheritance based on the code provided. I agree that saying Weapon is a Player makes little sense.

1
2
3
4
5
6
7
8
9
10
Player::Player( string pn, int ty, int lev, int str, 
 int wty, int wdur, int wlevReq)
 : weapon1(wty, wdur, wlevReq)
 {
 setPlayerName(pn);
 setType(ty);
 setLevel(lev);
 setStrength(str);
 weapon1 = new Weapon(wty, wdur, wlevReq)
 }

Because weapon1 is a pointer, line 3 is invalid. You can, however, do this:
1
2
3
4
5
6
7
8
9
Player::Player( string pn, int ty, int lev, int str, 
 int wty, int wdur, int wlevReq)
 : weapon1(new Weapon(wty, wdur, wlevReq))
 , name(pn)
 , type(ty)
 , level(lev)
 , strength(str)
)
 {}


Your syntax error may come from Player not knowing what Weapon is. Remember a compiler reads top to bottom and to include the appropriate headers.
Last edited on
Thanks a lot. I wasn't sure how to initialize it with a pointer

Now I only have 2 errors in my program, both being from
1
2
3
4
5
6
7
private:

		Weapon *pWeapon;		// default = 1, 25, 1
		string playerName;		// default = "No Name"
		int type;				// default = 1 (1, 2, 3, 4)
		int level;				// default = 1
		int strength;			// default = 25  
What errors? There is nothing syntactically wrong in that block, except that we don't know whether your compiler sees at this point the definitions of types "Weapon" and "string".

A forward declaration is enough for class Weapon (unless it is template-based), because compiler only needs to know the size of a pointer (which are all of same known size), but the "string" has to be defined in order to know how much memory is reserved for member variable 'playerName'.
Topic archived. No new replies allowed.