简体   繁体   English

不完整的C ++类型

[英]Incomplete type C++

I get the following error when I try to execute this code segment : "Menu does not name a type".I know its something to do with the circular references, but for the life of me I can't figure out what. 当我尝试执行此代码段时,出现以下错误:“菜单未命名类型”。我知道它与循环引用有关,但是对于我来说,我一辈子都搞不清楚。 Also, menu, go, and manager are repeatedly giving errors. 此外,菜单,执行和管理器都反复出现错误。 The code segments are posted below : 代码段发布在下面:

#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

Here's Menu : 这是菜单:

#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

Manager : 经理 :

#ifndef MANAG_H
#define MANAG_H

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

Can you see where the problem is? 您能看到问题出在哪里吗? Error message: 错误信息:

In file included from go.h:13:0, from manager.h:33, from manager.cpp:2: menu.h:28:11: error: field 'mng' has incomplete type 在go.h:13:0,manager.h:33,manager.cpp:2:menu.h:28:11中包含的文件中:错误:字段'mng'具有不完整的类型

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

The class Menu is being defined before it ever gets to the definition of class Manager . 在定义class Menu之前,必须先定义class Menu class Manager

However, class Menu needs a Manager but since the compiler doesn't know about Manager yet it doesn't know how big to make it. 但是, class Menu需要一个Manager但是由于编译器尚不知道Manager ,因此也不知道要制作多大的Manager

You could forward declare class Manager and make the mng member of Menu a pointer or reference: 您可以转发声明class Manager并使Menumng成员成为指针或引用:

class Manager;

class Menu {
    ...
    Manager* mng;

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

Here's a good explanation of circular references and how to fix them. 这是对循环引用以及如何修复它们的很好的解释。

It appears you are missing the semicolon at the end of the declaration of your Manager class in manger.h. 看来您在manger.h中的Manager类声明的末尾缺少分号。

You are also missing the #endif to close your include guard. 您还缺少#endif来关闭包含防护。

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

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