Not a typename?

Nov 29, 2012 at 3:37pm
Hi guys,
Ive got a problem with this class, I'm sure its something stupid again, it helps if someone takes a quick look at your code, as they can usually identify.

So when I try to create a player object, like this, it says "Player is not a typename". Yes I do have the header included aswell.
1
2
3
#include"Player.h"

Player* Player; 


^^ in header

then

1
2
3
Player = new Player();

//Do Stuff 


^^ in .cpp

I tried doing

Player* PLayer = new Player();

also in the .cpp

"Player.h"
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
#ifndef PLAYER_H
#define PLAYER_H
#pragma once
#include<SDL.h>
#include"Labels.h"

class Player
{
public:
	int getHealth();
	int getStrength();
	int getSpeed();
	int getDollars();
	void draw();
	Player(void);
	~Player(void);
	void handleSDLEvent(SDL_Event const &sdlEvent);
private:
	float xpos;
	float ypos;
	float xsize;
	float ysize;
	int health;
	int strength;
	int speed;
	int dollars;
	Labels* playerLabel;
};

#endif 



Player.cpp

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
78
79
80
81
82
83
#include "Player.h"


Player::Player()
{
	xpos = 0.0f;
	ypos = 0.0f;
	xsize = 0.15f;
	ysize = 0.15f;
	health = 10;
	speed = 10;
	strength = 10;
	dollars = 0;
};


Player::~Player(void)
{
};

int Player::getDollars()
{
	return dollars;
};

int Player::getHealth()
{
	return health;
};

int Player::getSpeed()
{
	return speed;
};

int Player::getStrength()
{
	return strength;
};

void Player::draw()
{
	glColor3f(1.0,1.0,1.0);
	glBegin(GL_POLYGON);
	glVertex3f (xpos, ypos, 0.0); // first corner
	glVertex3f (xpos+xsize, ypos, 0.0); // second corner
	glVertex3f (xpos+xsize, ypos+ysize, 0.0); // third corner
	glVertex3f (xpos, ypos+ysize, 0.0); // fourth corner
	glEnd();
	playerLabel = new Labels();
	playerLabel->textToTexture("player");
	playerLabel->DrawString(xpos+(xsize/2.0f), ypos+ysize);
};

void Player::handleSDLEvent(SDL_Event const &sdlEvent)
{
		if (sdlEvent.type == SDL_KEYDOWN)
	{
		//std::cout << "Scancode: " << sdlEvent.key.keysym.scancode  ;
		//std::cout <<  ", Name: " << SDL_GetKeyName( sdlEvent.key.keysym.sym ) << std::endl;
		switch( sdlEvent.key.keysym.sym )
		{
		case SDLK_UP:
		case 'w': case 'W': 
			ypos += 0.05f;
			break;
		case SDLK_DOWN:
		case 's': case 'S':
			ypos -= 0.05f;
			break;
		case SDLK_LEFT:
		case 'a': case 'A': 
			xpos -= 0.05f;
			break;
		case SDLK_RIGHT:
		case 'd': case 'D':
			xpos += 0.05f;
			break;
		default:
			break;
		}
	}
};



I assume there is something wrong in my constructor? as its not realising its a type? Not sure, I cant get it to work at all.
In the file I've got the Player.h included in, if I do Player:: it gives me all the things to call, so it recognises it as something.
Nov 29, 2012 at 3:43pm
Player* Player;
You cannot name variable like your class. Try to do that:
Player* player;
Nov 29, 2012 at 3:45pm
Well, you have a variable Player and a class with the same name. The compiler is not able to figure out if you mean the variable or the class Player
Nov 29, 2012 at 3:52pm
You can use the same name but then you will have to write class Player in a number of places to make it understand you mean the class and not the variable.
Player = new class Player;
Better to use different names.
Last edited on Nov 29, 2012 at 3:54pm
Nov 29, 2012 at 3:54pm
That worked guys, cheers! feeling silly.
Topic archived. No new replies allowed.