What I'm doing: I have two files: game.h and sound_controller.h. Both of them are having conflicts with each other. game.h requires having a SoundController, while sound_controller.h requires having a Game, so they're both including each other's header files.
The problem: game.h line 26:
error: field soundController has incomplete type 'SoundController'
I've included sound_controller.h in game.h, but it's saying the type is incomplete yet I've declared the class SoundController. So how do I fix this?
The code:
game.h:
#pragma once
/**
game.h
Handles highest level game logic
*/
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
#include <iostream>
#include "config.h"
#include "sound_controller.h"
#include "image_controller.h"
#include <stdarg.h>
class SoundController; //forward declaration
class Game {
ImageController imageController;
SoundController soundController;
SDL_Window* window = NULL;
bool running;
public:
Game();
bool init();
bool createWindow();
void update();
static void log(const char* format, ...);
};
sound_controller.h:
#pragma once
/**
sound_controller.h
Allows for manipulation with sound (sound effects and music)
*/
#include "config.h"
#include <SDL_mixer.h>
#include "Game.h"
class Game; //forward declaration
class SoundController {
bool init();
bool load_sound();
bool load_music();
void music_play();
void music_pause();
void music_stop();
};
sound_controller.cpp uses Game because it calls Game.h's static function: log.
EDIT:
Removed "#include game.h" from sound_controller.h. Now getting another error this time in sound_controller.cpp:
line 8 error: incomplete type 'Game' used in nested name specifier
sound_controller.cpp:
#include "sound_controller.h"
bool SoundController::init() {
bool success = true;
//Initialize SDL_mixer
if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 ) {
Game::log( "SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError() );
success = false;
}
return success;
}
EDIT2:
Solution was to put #include "game.h" in sound_controller.cpp.