SDL2, setup. Prototype does not match error!

Started by
2 comments, last by tds_creater 9 years, 6 months ago
Hi, Im not sure if this is the right forum, its my first post here.. Anyway Im trying to make a topdown game using SDL2. On my first project I edited a topdown example to be framerate dependent, better movement and view code. Was getting into collisiondetection.
But I felt the code got a bit messy so I decided to rewrite it from scratch, following a pdf guide named: SDL Game Development.
The code I got so far is basically the gameloop and getting a window to the screen.It worked untill I made a new game class and broke out functions from the main.
The error message I get is:
prototype for 'bool Game::init(const char*, int, int, int, int, int)' does not match any in class 'Game'|
candidate is: bool Game::init()|

I gone through it a few times and tried to refollow the guide. Maby its something simple I missed, but Im stuck. Any help is appreciated, thanks. The codes:

includes.h, (yea not all being used right now):

#ifndef INCLUDES_H_INCLUDED
#define INCLUDES_H_INCLUDED

#include<SDL.h>
#include<SDL_image.h>
#include<iostream>
#include<cstdlib>
#include<time.h>
#include<math.h>
#include <windows.h>

using namespace std;

#endif // INCLUDES_H_INCLUDED

Game.h:


#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

#include"includes.h"

#include "SDL.h"
class Game
{
public:
 Game();
 ~Game();
 bool init();
 void render();
 void update();
 void handleEvents();
 void clean();
 bool running() { return m_bRunning; }
private:
 SDL_Window* m_pWindow;
 SDL_Renderer* m_pRenderer;
 bool m_bRunning;
};
#endif // GAME_H_INCLUDED

Game.cpp:


#include "Game.h"
bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags)
{
 // attempt to initialize SDL
 if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
 {
        std::cout << "SDL init success\n";
        // init the window
        m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
        if(m_pWindow != 0) // window init success
        {
            std::cout << "window creation success\n";
            m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
            if(m_pRenderer != 0) // renderer init success
            {
                std::cout << "renderer creation success\n";
                SDL_SetRenderDrawColor(m_pRenderer,
                255,255,255,255);
            }
            else
            {
                std::cout << "renderer init fail\n";
                return false; // renderer init fail
            }
        }
        else
        {
            std::cout << "window init fail\n";
            return false; // window init fail
        }
    }
    else
    {
        std::cout << "SDL init fail\n";
        return false; // SDL init fail
    }
    std::cout << "init success\n";
    m_bRunning = true; // everything inited successfully,start the main loop
    return true;
}

void Game::render()
{
 SDL_RenderClear(m_pRenderer); // clear the renderer tothe draw color
 SDL_RenderPresent(m_pRenderer); // draw to the screen
}

void Game::handleEvents()
{
    SDL_Event event;
    if(SDL_PollEvent(&event))
    {
        switch (event.type)
        {
            case SDL_QUIT:
            m_bRunning = false;
            break;
            default:
            break;
        }
    }
}

void Game::clean()
{
 std::cout << "cleaning game\n";
 SDL_DestroyWindow(m_pWindow);
 SDL_DestroyRenderer(m_pRenderer);
 SDL_Quit();
}

main.cpp:


#include "Game.h"

// our Game object
Game* g_game = 0;
int main(int argc, char* argv[])
{
 g_game = new Game();
 g_game->init("Chapter 1", 100, 100, 640, 480, 0);
 while(g_game->running())
 {
 g_game->handleEvents();
 g_game->update();
 g_game->render();
 }
 g_game->clean();
 return 0;
}
Advertisement

In Game.h, change:


bool init();

to:


bool init(const char* title, int xpos, int ypos, int width, int height, int flags);

The arguments on the declaration (line in Game.h) and the definition (line in Game.cpp) must be the same. Actually, the name of the arguments can be different (I'm not sure why anyone would want that, but it's valid), but the number and types must match.

@DiegoSLTS is correct. You can also skip the parameter names in your header, as they are not needed by the compiler, they are there only for your convenience.

Some feedback:

In includes.h, you have


using namespace std;

In general, you should avoid the using namespace construct in headers, as it pollutes the global namespace for all source files that include the infected headers.

Also, you should include as little as possible in headers. If you have a lot of #include-s in headers, the compilation will slow down. AFAIR, you can forward declare most SDL types.

I don't know why the Game would be a pointer (you plan to make it a global variable?). I'd use a normal variable on the stack, and I would also run the cleanup code in Game's destructor.

Sorry for the late response, but Im back to this now! I think I was close to figuring that out, but somehow got confused. Thanks alot for the help, it did get a bit more clearer now!

After that I put a "//" before the: g_game->update(); , since I wherent having any update function yet. And I also added the code


Game::Game(){}
to game.cpp.
I removed the

using namespace std;

From the header also, as I understood Its not needed and you can just write for example std::cin. I will have to look what I need and wont need for the rest of the includes.

Im unsure why the game is a pointer, Im following the SDL Game Development.pdf guide, it was not explained. I been mostly only doing some highlevel programming before and Im a bit new to C++ so Im still figuring out how to do things yet!

Now it does seem to work as intended. My next step will be to add some drawing functions and get some game objects to the screen!

This topic is closed to new replies.

Advertisement