1

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

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Trista N
  • 403
  • 1
  • 4
  • 11
  • On the surface that looks OK, are you sure that is the code that is giving you the error? – juanchopanza Apr 25 '12 at 19:31
  • actually im getting another similiar error now, will post code! – Trista N Apr 25 '12 at 19:32
  • 1
    Could menu.h be somehow referencing go.h? This can cause that kind of error. – Benj Apr 25 '12 at 19:32
  • 2
    Try to reduce the code to the minimum number of lines that generate the errors - i.e. remove all members other than `Menu` and everything that is relevant, all unneeded includes, and edit the question. – Luchian Grigore Apr 25 '12 at 19:32
  • 1
    Try changing `Menu menu;` to `Menu* menu;` inside `class GO` – Forever Learner Apr 25 '12 at 19:32
  • Try adding a #error directive inside your menu.h just before the class Menu to make sure it is actually being included. You may be #define MENU_H in another include file. – David Nehme Apr 25 '12 at 19:34
  • You might also consider formatting your code a little better. It's a bit painful to read as is. – dusktreader Apr 25 '12 at 19:36
  • 1
    Also, what line number from which file does your compiler report the error with Menu. That would be very helpful – dusktreader Apr 25 '12 at 19:37
  • 2
    Please include **complete** compiler messages, with line numbers and file names. Also consider including line numbers in code listings, if they are longer than a couple dozen lines. It's no fun to reconstruct this information from hints and fragments. – n. m. could be an AI Apr 25 '12 at 19:38

2 Answers2

3

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.

Community
  • 1
  • 1
Dave Rager
  • 8,002
  • 3
  • 33
  • 52
  • Do I get rid of this by removing #include menu.h from go.h? - But each of those classes needs an object from the included class. – Trista N Apr 25 '12 at 19:52
1

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.

JohnMcG
  • 8,709
  • 6
  • 42
  • 49