【发布时间】:2018-03-18 21:30:34
【问题描述】:
我在做什么: 我有两个文件:game.h 和 sound_controller.h。他们俩都在互相冲突。 game.h 需要有 SoundController,而 sound_controller.h 需要有 Game,所以它们都包含彼此的头文件。
问题: game.h 第 26 行:
error: field soundController has incomplete type 'SoundController'
我在 game.h 中包含了 sound_controller.h,但它表示类型不完整,但我已经声明了 SoundController 类。那么我该如何解决呢?
代码:
游戏.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 使用 Game 是因为它调用 Game.h 的静态函数:log。
编辑:
从 sound_controller.h 中删除了“#include game.h”。这次在 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:
解决方案是将#include "game.h" 放入 sound_controller.cpp。
【问题讨论】:
标签: c++ forward-declaration incomplete-type