Inheritance problem I guess

ok... so I had a couple of problems with my code but now I have only one left...
I tried to figure it out for hours and also searched the internet but with no luck... I really hope you guys can help me.

so here's the error:

1
2
1>game.cpp(34): error C2039: 'keyPress' : is not a member of 'Sprite'
1>          c:\users\david\documents\visual studio 2010\projects\evilmonkeys\evilmonkeys\sprite.h(18) : see declaration of 'Sprite'


now here's the code...

game.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
#include "game.h"
#include "drawEngine.h"

#include <windows.h>

#include <conio.h>
#include <iostream>

using namespace std;

#define GAME_SPEED 33.33 // 30 FPS

bool Game::run(void)
{
	drawArea.createSprite(0, 'O');

	player = new Character(&drawArea, 0);
	
	char key = ' ';

	posx = 0;
	
	startTime = timeGetTime();
	frameCount = 0;
	lastTime = 0;
	
	while(key != 'q')
	{
		while(!getInput(&key))
		{
			timerUpdate();
		}

		player->keyPress(key);

		// cout << "Ai apasat: " << key << endl;
	}

	delete player;
	
	cout << frameCount / ((timeGetTime() - startTime) / 1000) << " FPS" << endl;
	cout << "Se inchide aplicatia..." << endl;

	return true;
}

bool Game::getInput(char *c)
{
	if (_kbhit())
	{
		*c = _getch();
		return true;
	}
	return false;
}

void Game::timerUpdate(void)
{
	double currentTime = timeGetTime() - lastTime;

	if (currentTime < GAME_SPEED)
		return;

	frameCount++;

	lastTime = timeGetTime();
}


game.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
#ifndef GAME_H
#define GAME_H

#include "drawEngine.h"
#include "character.h"

class Game
{
public:
	bool run(void);

protected:
	bool getInput(char *c);
	void timerUpdate(void);

private:
	Sprite *player;
	
	double frameCount;
	double startTime;
	double lastTime;

	int posx;

	DrawEngine drawArea;
};

#endif 


character.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef CHARACTER_H
#define CHARACTER_H

#include "sprite.h"

class Character : public Sprite
{
public:
	Character(DrawEngine *de, int s_index, float x = 1, float y = 1, int lives = 3, char u = 'w', char d = 's', char l = 'a', char r = 'd');
	
	virtual bool keyPress(char c);

protected:
	char upKey;
	char downKey;
	char leftKey;
	char rightKey;

};

#endif 


and finally sprite.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef SPRITE_H
#define SPRITE_H

#include "drawEngine.h"

enum
{
	SPRITE_CLASSID,
	CHARACTER_CLASSID
};
struct vector
{
	float x;
	float y;
};

class Sprite
{
public:
	Sprite(DrawEngine *de, int s_index, float x = 1, float y = 1, int i_lives = 1);
	~Sprite();

	vector getPosition(void);
	float getX(void);
	float getY(void);

	virtual void addLives(int num = 1);
	int getLives(void);
	bool isAlive(void);

	bool move(float x, float y);

protected:
	DrawEngine *drawArea;
	vector pos;
	int spriteIndex;
	int numLives;

	int classID;
	vector facingDirection;
	void draw(float x, float y);
	void erase(float x, float y);

};

#endif 


there are 5 more files but I hope this is enough... I don't want to take more space than I already did.

I really hope someone can help me. ask me for the rest of the code if you need it (main.cpp, drawEngine.h, drawEngine.cpp, sprite.cpp, character.cpp)
player is a Sprite* so you can only call the member functions of Sprite. Maybe what you want is player to be a Character*?
Last edited on
Hi chippzanuff93,

'keyPress' : is not a member of 'Sprite'

This is exactly as it says : there is no member function keyPress in the sprite class. The problem is that keypress is defined in the derived class, but you are using a pointer to the base class. Pointers to classes work the other way around : A pointer to a derived class is a valid pointer to the base class. So make player a pointer to a character object.






oh guys... thank you SO MUCH for clearing my mind... I guess I was tired or my brain got melted and turned into pasta or something... thank you very much, both of you! now it works as intended!
Topic archived. No new replies allowed.