繁体   English   中英

不完整的C ++类型

[英]Incomplete type C++

当我尝试执行此代码段时,出现以下错误:“菜单未命名类型”。我知道它与循环引用有关,但是对于我来说,我一辈子都搞不清楚。 此外,菜单,执行和管理器都反复出现错误。 代码段发布在下面:

#ifndef GO__H
#define GO__H

#include <SDL.h>
#include <iostream>
#include <string>
using std::cout; using std::endl; 
using std::string;

#include "ioManager.h"
#include "gui.h"
#include "clock.h"
#include "menu.h"

//class Menu;
class Go {
public:
    Go ();
    void play();
private:
    SDL_Surface *screen;
    Gui gui;
    Menu menu;

    void drawBackground() const;
    Go(const Go&);
    Go& operator=(const Go&);
};

#endif

这是菜单:

#ifndef MENU_H
#define MENU_H

#include <SDL.h>
#include <iostream>

#include "ioManager.h" 
#include "gui.h"
#include "clock.h"
#include "manager.h"

class Menu {
public:
    Menu ();
    void play();

private:
    const Clock& clock;
    bool env;

    SDL_Surface *screen;
    Gui gui;
    Manager mng;

    void drawBackground() const;
    Menu(const Menu&);
    Menu& operator=(const Menu&);
};

#endif

经理 :

#ifndef MANAG_H
#define MANAG_H

#include "go.h"
class Manager { 
  Go go;
  //other code
}

您能看到问题出在哪里吗? 错误信息:

在go.h:13:0,manager.h:33,manager.cpp:2:menu.h:28:11中包含的文件中:错误:字段'mng'具有不完整的类型

manager.h包括go.h ,其中包括menu.h ,其中manager.h ...

在定义class Menu之前,必须先定义class Menu class Manager

但是, class Menu需要一个Manager但是由于编译器尚不知道Manager ,因此也不知道要制作多大的Manager

您可以转发声明class Manager并使Menumng成员成为指针或引用:

class Manager;

class Menu {
    ...
    Manager* mng;

    // or this:
    //Manager& mng; 
    ...

这是对循环引用以及如何修复它们的很好的解释。

看来您在manger.h中的Manager类声明的末尾缺少分号。

您还缺少#endif来关闭包含防护。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM