【问题标题】:Undeclared Identifier-Declarations Present In Header, But Unidentified In Class Source标头中存在未声明的标识符 - 声明,但在类源中未识别
【发布时间】:2015-05-03 16:53:07
【问题描述】:

所以,我遇到了一个可能很简单的问题,但我似乎无法弄清楚是什么原因造成的。

我有一个名为“Game”的 C++ 类,由 Game.h 中的类声明组成 以及 Game.cpp 中的源定义。

我已在“Game.cpp”中包含“Game.h”,但由于某种原因,Visual Studio 似乎无法识别 Game.h 中的类声明。

如果我能得到任何帮助来弄清楚为什么会出现这种情况,以及为什么会出现以下错误,我将不胜感激: 'MyCharacter' : undeclared identifier'HandleKeyPressed' : identifier not found

游戏.h:

------------------------------------------------------------------------------------------------------------------
#pragma once
#ifndef Game_Header
#define Game_Header


#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Character.h"
#include "FloorPlatform.h"

class Game
{
public:
        Game();
        ~Game();
        int Run();
        void HandleKeyPressed(sf::Keyboard::Key);
        Character MyCharacter;
};

#endif // !Game_Header

Game.cpp 精简版

#include "Game.h"


Game::Game()
{
}


Game::~Game()
{
}

int Run(){
        sf::RenderWindow MainGameWindow(sf::VideoMode::getDesktopMode(), "A Test Game");

        //Start Game Loop
        while (MainGameWindow.isOpen()){
                while (MainGameWindow.pollEvent(event)){
                        //Handle some other events here...
                        if (event.type == sf::Event::KeyPressed){
                                HandleKeyPressed(event.key.code);
                        }
                }


                MainGameWindow.clear(sf::Color::White);

                MyCharacter.Instance.Circle.setPosition(MyCharacter.PlayerLocation);
                MainGameWindow.draw(MyCharacter.Instance.Circle);
                MainGameWindow.display();

        }

        return 0;
}

void HandleKeyPressed(sf::Keyboard::Key PressedKey){
        switch (PressedKey)
        {
        case sf::Keyboard::A:
             MyCharacter.PlayerLocation.x -= 16;
        break;

        }
}

完整的代码可以在这里找到: http://pastebin.com/x6KhDxgL

在此先感谢您提供的任何帮助。

【问题讨论】:

    标签: c++ header identifier undeclared-identifier


    【解决方案1】:

    试试

    int Game::Run()
    

    而不是

    int Run()
    

    在 Game.cpp 中,HandleKeyPressed 也是如此,因为它是 Game 类的方法。根据您的Character.h,您可能还需要初始化MyCharacter

    【讨论】:

    • 哇...我觉得自己像个白痴。因此,只需将方法更改为 Game 命名空间即可解决问题。为什么不声明命名空间会导致类的其他成员无法识别?谁能解释一下?另外,为了清楚起见:我按照建议将 int Run() 和 void HandleKeyPressed 更改为 int Game::Run() 和 void Game::HandleKeyPressed。
    • 因为您在类定义之外声明方法定义。如果您在Game.h 中的Game 定义中编写了整个方法,则可能没问题。由于您是在课堂外进行的,因此您需要使用::(范围运算符)。而且#inlude "Game.h" 与使用命名空间相同。
    • 啊...这有点清楚了,但是,为什么这会影响“MyCharacter”标识符?我认为因为标识符是在 Game.h 中声明的,所以它应该可以在整个 Game.cpp 中使用。我很感激帮助。我很高兴我的东西有效,我只是真的想更好地理解这一点,所以我以后不必问那么多愚蠢的问题。谢谢。
    • 我认为这就像声明 int a - 您没有为 MyCharacter 部分分配任何值。但是,我只是在猜测这部分,因为我无法运行您的整个代码(包括您的头文件)。如果这是问题所在,请在 Game 构造函数中为其赋值。另外,如果此答案对您有帮助,请标记为已接受。
    【解决方案2】:
    void HandleKeyPressed(sf::Keyboard::Key PressedKey){
    

    在 Game.cpp 中应该是

    void Game::HandleKeyPressed(sf::Keyboard::Key PressedKey){
    

    int run() 也一样

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      相关资源
      最近更新 更多