带参数的变量和函数中找不到标识符

时间:2015-03-02 23:42:40

标签: c++ visual-studio-2012 identifier

我正在使用VS ultimate 2012,并试图创建一个类,但所有带参数的变量和函数都会返回“找不到标识符”。

函数update(),run()等完美无缺。

我尝试在.cpp文件中定义,例如:mIsMovingUp(false)和Game :: mIsMovingUp,但都没有用,我真的被困在这里。

错误:

game.cpp(24): error C2039: 'handlePlayerInput' : is not a member of 'Game'
game.h(7) : see declaration of 'Game'
game.cpp(27): error C2065: 'mIsMovingUp' : undeclared identifier
game.cpp(29): error C2065: 'mIsMovingDown' : undeclared identifier
game.cpp(31): error C2065: 'mIsMovingLeft' : undeclared identifier
game.cpp(33): error C2065: 'IsMovingRight' : undeclared identifier
game.cpp(62): error C2065: 'mIsMovingUp' : undeclared identifier
game.cpp(64): error C2065: 'mIsMovingDown' : undeclared identifier
game.cpp(66): error C2065: 'mIsMovingLeft' : undeclared identifier
game.cpp(68): error C2065: 'mIsMovingRight' : undeclared identifier

代码:

//game.h
#ifndef GAME_H
#define GAME_H

#include <SFML/Graphics.hpp>

class Game
{
    public:
        Game();
        void run();

    private:
        void processEvents();
        void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
        void update();
        void render();

    private:
        sf::RenderWindow mWindow;
        sf::CircleShape mPlayer;

        bool mIsMovingUp;
        bool mIsMovingDown;
        bool mIsMovingRight;
        bool mIsMovingLeft;


};

#endif

//game.cpp
#include "..\Headers\game.h"

Game::Game()
    : mWindow(sf::VideoMode(640, 480), "Beginning")
    , mPlayer()


{
    mPlayer.setRadius(40.f);
    mPlayer.setPosition(100.f, 100.f);
    mPlayer.setFillColor(sf::Color::Cyan);
}

void Game::run()
{
    while (mWindow.isOpen())
    {
        processEvents();
        update();
        render();
    }
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed) //error
{
    if (key == sf::Keyboard::W)
        mIsMovingUp = isPressed;               //error
    else if (key == sf::Keyboard::S)
        mIsMovingDown = isPressed;             //error
    else if (key == sf::Keyboard::A)
        mIsMovingLeft = isPressed;             //error
    else if (key == sf::Keyboard::D)
        IsMovingRight = isPressed;             //error
}

void Game::processEvents()
{
    sf::Event event;
    while (mWindow.pollEvent(event))
    {
        switch (event.type)
        {
        case sf::Event::KeyPressed:
            handlePlayerInput(event.key.code, true);
            break;
        case sf::Event::KeyReleased:
            handlePlayerInput(event.key.code, false);
            break;
        case sf::Event::Closed:
            mWindow.close();
            break;
        }
    }
}

void Game::update()
{
    sf::Vector2f movement(0.f, 0.f);

    if (mIsMovingUp)                   //error
        movement.y -= 1.f;
    if (mIsMovingDown)                 //error
        movement.y += 1.f;
    if (mIsMovingLeft)                 //error
        movement.x -= 1.f;
    if (mIsMovingRight)                //error
        movement.x += 1.f;

    mPlayer.move(movement);
}

void Game::render()
{
    mWindow.clear();
    mWindow.draw(mPlayer);
    mWindow.display();
}

//main.cpp
#include "game.h"

int main()
{
    Game game;
    game.run();
}

1 个答案:

答案 0 :(得分:1)

#include "..\Headers\game.h"很可疑。您应该#include "game.h"并正确设置包含路径。

也许它实际上包含的文件的旧版本没有在Game类中定义的那些东西。

要测试这个,你可以把一些垃圾放到game.h中,看看它是否会导致game.cpp给出编译错误