weird errors *very long post*

I'm following the tutorials by 3dbuzz and am on character class. so far everything has made sense. I did what they did, I think, and am getting some weird errors.

Compiler says 'C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp||In constructor 'Character::Character(DrawEngine*, int, float, float, int, char, char, char, char)':|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp|7|error: 'upKey' was not declared in this scope|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp|8|error: 'downKey' was not declared in this scope|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp|9|error: 'leftKey' was not declared in this scope|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp|10|error: 'rightKey' was not declared in this scope|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp||In member function 'virtual bool Character::keyPress(char)':|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp|17|error: 'upKey' was not declared in this scope|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp|21|error: 'downKey' was not declared in this scope|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp|25|error: 'rightKey' was not declared in this scope|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp|29|error: 'leftKey' was not declared in this scope|
C:\Users\1355710\Desktop\C++ projects\evilMonkeys\character.cpp|33|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 8 errors, 1 warnings (0 minutes, 0 seconds) ===|

and help would be greatly appreciated :D

1
2
3
4
5
6
7
8
9
10
  #include "game.h"
int main()
{//start of main

    Game gameHeart;

    gameHeart.run();

    return 0;
}//end of main 


that was my main.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
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

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

class Game
{
public:
    bool run(void);
private:
    Character *player;

    double frameCount;
    double startTime;
    double lastTime;

    int posx;

    DrawEngine drawArea;

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



#endif // GAME_H_INCLUDED 


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

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

using namespace std;


//this limits FPS. to find what number to use, divide 1000 by the target fps. use the answer here
const int GAME_SPEED = (1000/30);


bool Game::run(void)
{
    drawArea.createSprite(0, '$'); //the char is the evil monkey sprite

    player = new Character(&drawArea, 0);

    char key = ' ';

    startTime = timeGetTime();
    frameCount = 0;
    lastTime = 0;


    posx = 0;

    while ( key != 'q')
        {
            while (!getInput(&key))
            {
                timerUpdate();
            }
            player->keyPress(key);
        }
        delete player;

        cout << frameCount / ((timeGetTime() - startTime) / 1000)<< "fps" << endl;
        cout << "End of the game." << endl;
        return true;
}

bool Game::getInput(char *c)
{
    if (kbhit())
    {
        *c = getch();
        return true;
    }

   return false;
}

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

    if (currentTime < GAME_SPEED)
        return;

       /* player ->move(1,1); //moves player 1 to right and 0 in y

        drawArea.eraseSprite(posx, 5); //erase sprite as it goes
        posx = (posx + 1) % 80;
        drawArea.drawSprite(0, posx, 5); //the last two numbers are the evil monkeys cordinates */

    frameCount ++;
    lastTime = timeGetTime();
}


this is game.cpp. I don't think I need to include the draw engine, so here is the sprite class' header file

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
#ifndef SPRITE_H_INCLUDED
#define SPRITE_H_INCLUDED

#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);

    virtual 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 // SPRITE_H_INCLUDED 


and now here is sprite.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
#include "sprite.h"

Sprite::Sprite(DrawEngine *de, int s_index, float x, float y, int i_lives)
{
    drawArea = de;

    pos.x = x;
    pos.y = y;

    spriteIndex = s_index;

    numLives = i_lives;

    facingDirection.x = 1;
    facingDirection.y = 0;

    classID = SPRITE_CLASSID;
}

Sprite::~Sprite()
{
    //erase the dieing sprite
    erase(pos.x, pos.y);
}

vector Sprite::getPosition(void)
{
    return pos;
}

float Sprite::getX(void)
{
    return pos.x;
}

float Sprite::getY(void)
{
    return pos.y;
}

void Sprite::addLives(int num)
{
    numLives += num;
}
int Sprite::getLives(void)
{
    return numLives;
}
bool Sprite::isAlive(void)
{
    return (numLives > 0);
}

bool Sprite::move(float x, float y)
{
    //erase sprite
    erase(pos.x, pos.y);

    pos.x += x;
    pos.x += y;

    facingDirection.x = x;
    facingDirection.y = y;
    //draw sprite
    draw(pos.x, pos.y);

    return true;
}
    void Sprite::draw(float x, float y)
    {
        drawArea ->drawSprite(spriteIndex, (int)x, (int)y);
    }
    void Sprite::erase(float x, float y)
    {
        drawArea ->eraseSprite((int)x, (int)y);
    }


and here is character.h

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

#include "sprite.h"

class Character : public Sprite
{
public:
    Character(DrawEngine *de, int s_index, float x = 1, float y = 1,
              int lives = 3, char up_key = 'w', char down_key = 's', char left_key = 'a', char right_key = 'd');

    virtual bool keyPress(char c);

protected:
    char up_key;
    char down_key;
    char right_key;
    char left_key;
};


#endif // CHARACTER_H_INCLUDED 


and lastly, character.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
#include "character.h"

Character::Character(DrawEngine *de, int s_index, float x, float y, int lives,
                     char u, char d, char l, char r)
                     : Sprite(de, s_index, x, y, lives)
{
    upKey = u;
    downKey = d;
    leftKey = l;
    rightKey = r;

    classID = CHARACTER_CLASSID;
}

bool Character::keyPress(char c)
{
    if (c == upKey)
    {
        return move(0, -1);
    }
    else if (c == downKey)
    {
        return move(0, 1);
    }
    else if ( c == rightKey)
    {
        return move(1, 0);
    }
    else if (c == leftKey)
    {
        return move(-1, 0);
    }
}


Thank you!
in character class its spelt up_key

 
char up_key;


in the constructor its spelt upKey

 
upKey = u;


If you read the error messages you will find they are not weird at all
your members declared as:
char up_key;
and you use them as:
upKey = u; c == upKey
They are different.
When I changed it it ran, but 'w' and 's' won't work. 'S' moves the $ character right, and 'w' moves the character left. 'W' needs to move it up, and 's' needs to move it down... How do I fix this new issue?
Last edited on
Character.cpp now reads

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

Character::Character(DrawEngine *de, int s_index, float x, float y, int lives,
                     char u, char d, char l, char r)
                     : Sprite(de, s_index, x, y, lives)
{
    up_key = u;
    down_key = d;
    left_key = l;
    right_key = r;

    classID = CHARACTER_CLASSID;
}

bool Character::keyPress(char c)
{
    if (c == up_key)
    {
        return move(0, -1);
    }
    else if (c == down_key)
    {
        return move(0, 1);
    }
    else if ( c == right_key)
    {
        return move(1, 0);
    }
    else if (c == left_key)
    {
        return move(-1, 0);
    }
}
Topic archived. No new replies allowed.