Problem with two header files including each other

Started by
2 comments, last by PeterWelzien 13 years, 11 months ago
Hi. I'm having problems with two header files including each other. App.h

#pragma once
#include "Sudoku.h"
#include "MainFrame.h"
#include <wx/wx.h>

#define BOARD_SIZE 3

class App : public wxApp
{
  public:
  enum MenuId
  {
    NEW_VERY_EASY,
    NEW_EASY,
    NEW_MEDIUM,
    NEW_HARD
  };
  App();
  ~App();
  virtual bool OnInit();
  void onNewVeryEasy();
  void onNewEasy();
  void onNewMedium();
  void onNewHard();
  void onAbout();
  void onQuit();

  private:
  Sudoku *sudoku;
  MainFrame *mainFrame; //                                      <--LINE 32
};

MainFrame.h

#pragma once
#include "App.h"
#include <wx/wx.h>

class MainFrame : public wxFrame
{
  public:
  MainFrame();
  ~MainFrame();

  private:
  App *app; // Pointer to main application object                <--LINE 12
  wxMenuBar *menubar;
  wxMenu *fileMenu;
  wxMenu *newMenu;
};

Visual Studio 2010 gives the following errors: App.h(32): error C2143: syntax error : missing ';' before '*' App.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int App.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int MainFrame.h(12): error C2143: syntax error : missing ';' before '*' MainFrame.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int MainFrame.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int I believe that the problem is that the two files include each other. How can I solve this problem?
/Peter Welzien
Advertisement
Required reading.
Generally if you are only have a pointer to an object in a class definition, you're better off forward defining it, rather than including the header.


For example:

App.h
#pragma once// forward definitionsclass Suduko;class MainFrame;class App { /*blah blah*/ }


MainFrame.h
#pragma once#include <wx/wx.h>// forward definitionsclass App;class MainFrame : public wxFrame { /*blah blah*/ }


You should be able to safely include both headers inside the c/cpp files that need to actually do something with the classes.

Thanks!

I've been reading this site for a while, but this was my first post. And I got an answer after only four minutes! I love this place.
/Peter Welzien

This topic is closed to new replies.

Advertisement