我只是想在c ++和codeblocks中获得一些小原型,但我不知道为什么,每个方法和参数都会显示为未在范围内声明。到目前为止,这是我的代码:
Main.cpp的:
#include <iostream>
#include "Constantes.h"
#include "Game.h"
int main(int argc, char* argv[])
{
std::cout <<"Incializando "<<TITULO<<" en "<<ALTO<<"x"<<ANCHO<<" a "<<FPS<<" FPS"<<"..."<< std::endl;
Game juego;
std::cout << "Finalizando Juego..." << std::endl;
return 0;
}
Game.h:
#include "../Constantes.h"
#include <SDL2/SDL.h>
class Game
{
public:
Game();
virtual ~Game();
protected:
private:
void Init();
void Bucle();
void Exit();
bool gameOver;
SDL_Window * ventana;
SDL_Renderer * render;
};
Game.cpp:
#include "Game.h"
Game::Game()
{
Init();
}
Game::~Game()
{
//dtor
}
void Game::Init()
{
gameOver = false;
SDL_Init(SDL_INIT_EVERYTHING);
ventana = SDL_CreateWindow(TITULO,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,ANCHO,ALTO,SDL_WINDOW_SHOWN);
SDL_CreateRenderer(ventana,0,SDL_RENDERER_ACCELERATED);
}
void Game::Bucle()
{
while(!gameOver)
{
SDL_RenderClear();
}
}
Constantes.h:
#define ALTO 640
#define ANCHO 480
#define FPS 60
#define TITULO "V0.01"
这是我的错误列表:
||=== Build: Debug in Gameu (compiler: GNU GCC Compiler) ===|
C:\Users\SIMU\Desktop\ppm\Gameu\Gameu\src\Game.cpp||In constructor 'Game::Game()':|
C:\Users\SIMU\Desktop\ppm\Gameu\Gameu\src\Game.cpp|5|error: 'gameOver' was not declared in this scope|
C:\Users\SIMU\Desktop\ppm\Gameu\Gameu\src\Game.cpp||In member function 'void Game::Init()':|
C:\Users\SIMU\Desktop\ppm\Gameu\Gameu\src\Game.cpp|16|error: 'gameOver' was not declared in this scope|
C:\Users\SIMU\Desktop\ppm\Gameu\Gameu\src\Game.cpp|22|error: no 'void Game::Bucle()' member function declared in class 'Game'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
我可以看到gameOver几乎在Game类的私有部分声明。我不知道出了什么问题。
答案 0 :(得分:0)
您未在Constantes.h
中加入Game.cpp
。
嗯,你至少要尝试一下。当您只需要#include "../Constantes.h"
时,不知道为什么Game.h
中有#include "Constantes.h"
。您是否尝试从上面的几个文件夹中获取不同的文件?
答案 1 :(得分:0)
我无法解决问题,但我找到了解决方案。我不知道它为什么生成,但是,我删除了Game.cpp和Game.h并再次创建了类(创建了新的),复制/粘贴代码及其工作。 _(ツ)_ /¯