我是编程的新手,我正在尝试按照我买的书...但即使按照本书的代码,我也无法让程序运行。
========(main.cpp)========
#include "Game.h"
Game* g_game = 0;
int main(int argc, char* argv[])
{
g_game = new Game();
g_game -> init("Titulo", 100, 100, 800, 600, 0);
while(g_game->running())
{
g_game->handleEvents();
g_game->update();
g_game->render();
}
g_game->clean();
return 0;
}
========(Game.cpp)========
#include "Game.h"
using namespace std;
bool Game::init(const char* titulo, int xpos, int ypos, int altura, int largura, int flags)
{
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
m_pWindow = SDL_CreateWindow(titulo, xpos, ypos, altura, largura, flags);
if (m_pWindow != 0)
{
g_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer != 0)
{
SDL_SetRenderDrawColor(m_pRenderer, 255, 255,255, 255);
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
m_bRunning = true; //Começar o loop
return true;
}
========(Game.h)========
#ifndef _Game_
#define _Game_
#include <SDL.h>
class Game
{
public:
Game(){}
~Game(){}
void init() {m_bRunning = true; }
void render(){}
void update() {}
void handleEvents(){}
void clean(){}
bool running() { return m_bRunning; }
private:
SDL_Window* m_pWindow;
SDL_Window* m_pRenderer;
bool m_bRunning;
};
#endif /* defined(_Game_) */
但是当我尝试编译时,我收到了以下错误:
[Error] no matching function for call to 'Game::init(const char [7], int, int, int, int, int)'
[Note] candidate is: In file included from main.cpp
[Note] void Game::init()
[Note] candidate expects 0 arguments, 6 provided
有人可以帮助我解释为什么会这样吗?我阅读了有关默认构造缺失的其他主题,但无法弄明白。
答案 0 :(得分:2)
你的类声明(和定义)
void init() {m_bRunning = true; }
但是没有声明您稍后尝试定义的重载。加上这个:
bool init(const char* titulo, int xpos, int ypos, int altura, int largura, int flags);
上课。请注意声明末尾的分号。
一般来说,您需要在课程中声明班级的所有成员。定义。您可以决定直接定义它们(如init()
方法)或稍后(如第二个{{1}}方法),但是一旦定义了类,您就不能简单地在以后添加新成员在其他地方定义它们。