gpt4 book ai didi

c++ - 在带参数的变量和函数中找不到标识符

转载 作者:行者123 更新时间:2023-11-30 05:46:42 25 4
gpt4 key购买 nike

我正在使用 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();
}

最佳答案

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

也许它实际上包含了一个旧版本的文件,该文件没有在 Game 类中定义这些内容。

要对此进行测试,您可以将一些垃圾放入 game.h 中,看看它是否会导致 game.cpp 给出编译器错误

关于c++ - 在带参数的变量和函数中找不到标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28821526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com