Was ich mache: Ich habe zwei Dateien: game.h und sound_controller.h. Beide haben Konflikte miteinander. game.h benötigt einen SoundController, während sound_controller.h ein Spiel benötigt, also enthalten beide die Header-Dateien des jeweils anderen.Unvollständiger Typ, aber ich verwende Vorwärtsdeklarationen?
Das Problem: game.h Linie 26:
error: field soundController has incomplete type 'SoundController'
Ich habe sound_controller.h in game.h enthalten, aber es sagt der Typ ist unvollständig noch habe ich die Klasse SoundController erklärt . Also, wie repariere ich das?
Der 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 Spiel verwendet, weil es Game.h statische Funktion aufruft: log .
EDIT:
entfernt "# include game.h" von sound_controller.h. Jetzt bekommen einen anderen Fehler diesmal 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:
Lösung war #include "game.h" in sound_controller setzen .cpp.
Mögliches Duplikat von [Beheben von Buildfehlern aufgrund der zirkulären Abhängigkeit zwischen Klassen] (https://stackoverflow.com/questions/625799/resolve -build-Fehler-wegen-Kreis-Abhängigkeit-zwischen-Klassen) – SirGuy