我有2个文件互相使用,所以我尝试将每个文件都包含在每个文件中,这不起作用,因为文件会包含它们自己。我尝试将相关的include语句放在标题保护中,但这使得编译器认为所有变量名都是类型标识符。
这是我在头部守卫内部所拥有的东西
//MenuItem.h
#include <SDL.h>
#include <string>
#ifndef MENU_ITEM_H //avoid multiple inclusion
#define MENU_ITEM_H
#include "window.h"
class MenuItem {
bool selected = false;
window containerWindow;
etc
Window.h在其标题保护中包含MenuItem.h。
在MenuItem.h中我收到错误,例如
Error 1 error C2146: syntax error : missing ';' before identifier 'containerWindow'
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
当我把它们放在护头外面时(如此)
//MenuItem.h
#include <SDL.h>
#include <string>
#include "window.h"
#ifndef MENU_ITEM_H //avoid multiple inclusion
#define MENU_ITEM_H
class MenuItem {
bool selected = false;
window containerWindow;
etc
我得到以下
Error 1 error C1014: too many include files : depth = 1024
我不确定如何解决它。
答案 0 :(得分:1)
使用前向声明避免循环包含:在类window
中使用MenuItem
指针而不是对象。
替换:
#include "window.h"
class MenuItem {
bool selected = false;
window containerWindow;
etc
人:
class window; // or struct window if window is a struct
class MenuItem {
bool selected = false;
window* containerWindow;
etc
此外,如评论所示,请确保#ifdef
有#endif
,但我认为这不会导致您报告的问题。