Home > other >  Error compiling implementation and header file
Error compiling implementation and header file

Time:09-10

I'm trying to add the definitions for my member functions in game.h but I'm getting compiler errors that say the void functions are previously defined. The same error are output from game.h and also from game.cpp. It's almost like the pragma once is not working?? Im getting an error in Rider also saying the same thing in the game.cpp file but only for void functions.

game.h

#pragma once

#include "SDL2/SDL.h"


class Game{
public:

    Game() {}
    ~Game() {}

    bool init(const char* title, int xPos, int yPos, int width,int height, int flags);



    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{};
};

game.cpp

#include <iostream>
#include "game.h"


bool Game::init(const char* title, int xPos, int yPos, int width, int height, int flags){

    if(SDL_Init(SDL_INIT_EVERYTHING) == 0){
        std::cout << "SDL init success\n";

        m_pWindow = SDL_CreateWindow(title, xPos, yPos, width, height, flags);

        if(m_pWindow != nullptr){
            std::cout << "window creation success\n";
            m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);

            if(m_pRenderer != nullptr){
                std::cout << "renderer creation success\n";
                SDL_SetRenderDrawColor(m_pRenderer,255,255,255,255);
            }
            else{
                std::cout << "renderer failed\n";
                return false;
            }
        }
        else{
            std::cout << "SDL init fail\n";
            return false;
        }

        std::cout << "Successful initialization\n";
        m_bRunning = true;
    }
}

void Game::render() {
    SDL_RenderClear(m_pRenderer);
    SDL_RenderPresent(m_pRenderer);
}


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 << "clenaing up\n";
    SDL_DestroyWindow(m_pWindow);
    SDL_DestroyRenderer(m_pRenderer);
    SDL_Quit();
}

main.cpp

#include "game.h"

Game* game = 0;

int main(){
    game = new Game();
    game->init("Chapter 1", 100, 100, 640, 480, 0);

    while (game->running()){
        game->handleEvents();
        game->update();
        game->render();
    }

    game->clean();

    return 0;

}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)
project(sdl2_demo VERSION 0.1.0)

add_executable(sdl2_demo ./src/main.cpp src/game.cpp src/game.h)

target_link_libraries(sdl2_demo SDL2)

error

====================[ Build | sdl2_demo | Debug ]===============================
/app/extra/clion/bin/cmake/linux/bin/cmake --build /home/benmarr/repos/sld2_demo/cmake-build-debug --target sdl2_demo -j 12
[2/3] Building CXX object CMakeFiles/sdl2_demo.dir/src/game.cpp.o
FAILED: CMakeFiles/sdl2_demo.dir/src/game.cpp.o 
/usr/bin/c     -g -MD -MT CMakeFiles/sdl2_demo.dir/src/game.cpp.o -MF CMakeFiles/sdl2_demo.dir/src/game.cpp.o.d -o CMakeFiles/sdl2_demo.dir/src/game.cpp.o -c /home/benmarr/repos/sld2_demo/src/game.cpp
/home/benmarr/repos/sld2_demo/src/game.cpp:39:6: error: redefinition of ‘void Game::render()’
   39 | void Game::render() {
      |      ^~~~
In file included from /home/benmarr/repos/sld2_demo/src/game.cpp:6:
/home/benmarr/repos/sld2_demo/src/game.h:20:10: note: ‘void Game::render()’ previously defined here
   20 |     void render(){};
      |          ^~~~~~
/home/benmarr/repos/sld2_demo/src/game.cpp:45:6: error: redefinition of ‘void Game::handleEvents()’
   45 | void Game::handleEvents() {
      |      ^~~~
In file included from /home/benmarr/repos/sld2_demo/src/game.cpp:6:
/home/benmarr/repos/sld2_demo/src/game.h:22:10: note: ‘void Game::handleEvents()’ previously defined here
   22 |     void handleEvents(){};
      |          ^~~~~~~~~~~~
/home/benmarr/repos/sld2_demo/src/game.cpp:58:6: error: redefinition of ‘void Game::clean()’
   58 | void Game::clean() {
      |      ^~~~
In file included from /home/benmarr/repos/sld2_demo/src/game.cpp:6:
/home/benmarr/repos/sld2_demo/src/game.h:23:10: note: ‘void Game::clean()’ previously defined here
   23 |     void clean(){};
      |          ^~~~~
/home/benmarr/repos/sld2_demo/src/game.cpp: In member function ‘bool Game::init(const char*, int, int, int, int, int)’:
/home/benmarr/repos/sld2_demo/src/game.cpp:37:1: warning: control reaches end of non-void function [-Wreturn-type]
   37 | }
      | ^
ninja: build stopped: subcommand failed.

CodePudding user response:

You have an implementation in your header for some functions (empty):

void handleEvents(){};

And then an implementation in your source.

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

Update the header to just be a declaration:

void handleEvents();

CodePudding user response:

In your game.h file, you're creating empty implementation of the class methods, however in your game.cpp file you are creating full implementations of the same class methods.

Transform from

    void render(){};
    void handleEvents(){};
    void clean(){};

To this

    void render();
    void handleEvents();
    void clean();

All you need is the defenition of the function in your header file, then you can define them in your .cpp file

  • Related