简体   繁体   中英

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

manager.h includes go.h which includes menu.h which includes manager.h ...

The class Menu is being defined before it ever gets to the definition of 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.

You could forward declare class Manager and make the mng member of Menu a pointer or reference:

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.

You are also missing the #endif to close your include guard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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