没有匹配的函数调用- candidate期望0参数,6提供。

时间:2021-02-18 18:48:39

I'm new to programming and I'm trying to follow a book I bought... but even following the book's code I can't make the program to run.

我是编程新手,我正试着读我买的书……但是,即使按照本书的代码,我也不能让程序运行。

======== (main.cpp) ========

= = = = = = = =(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) ========

= = = = = = = =(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) ========

= = = = = = = =(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_) */

But when I'm trying to compile I got the following error:

但是当我试图编译时,我得到了以下错误:

[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

Can someone help me and explain why did this happens? I read other topics about the missing of a default construct, but couldn't figure it out.

有人能帮我解释一下为什么会这样吗?我阅读了其他关于缺省构造缺失的主题,但无法找到答案。

1 个解决方案

#1


2  

Your class declares (and defines)

您的类声明(并定义)

void init() {m_bRunning = true; }

but there is no declaration for the overload you are later on trying to define. Add this:

但是,对于您稍后试图定义的重载,并没有声明。添加:

bool init(const char* titulo, int xpos, int ypos, int altura, int largura, int flags);

to the class. Note the semicolon at the end of the declaration.

类。注意声明末尾的分号。

Generally speaking, you need to declare all members of a class in the class' definition. You can decide to define them directly (like your init() method) or later on (like the second init(...) method), but once a class has been defined, you can not simply add new members later on by defining them somewhere else.

一般来说,您需要在类的定义中声明类的所有成员。您可以决定直接定义它们(比如init()方法)或稍后(比如第二个init(…)方法),但是一旦定义了一个类,您就不能简单地通过在其他地方定义它们来添加新成员。

#1


2  

Your class declares (and defines)

您的类声明(并定义)

void init() {m_bRunning = true; }

but there is no declaration for the overload you are later on trying to define. Add this:

但是,对于您稍后试图定义的重载,并没有声明。添加:

bool init(const char* titulo, int xpos, int ypos, int altura, int largura, int flags);

to the class. Note the semicolon at the end of the declaration.

类。注意声明末尾的分号。

Generally speaking, you need to declare all members of a class in the class' definition. You can decide to define them directly (like your init() method) or later on (like the second init(...) method), but once a class has been defined, you can not simply add new members later on by defining them somewhere else.

一般来说,您需要在类的定义中声明类的所有成员。您可以决定直接定义它们(比如init()方法)或稍后(比如第二个init(…)方法),但是一旦定义了一个类,您就不能简单地通过在其他地方定义它们来添加新成员。