Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problem with two header files including each other


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 PeterWelzien   Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 May 2010 - 07:32 AM

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

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9566

Like
0Likes
Like

Posted 26 May 2010 - 07:36 AM

Required reading.

#3 Noggs   Members   -  Reputation: 141

Like
0Likes
Like

Posted 26 May 2010 - 07:40 AM

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 definitions
class Suduko;
class MainFrame;

class App { /*blah blah*/ }



MainFrame.h

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

// forward definitions
class 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.



#4 PeterWelzien   Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 May 2010 - 08:07 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS