Results 1 to 2 of 2

Thread: confusing situation

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2000
    Posts
    34

    confusing situation

    I have a little problem that I was hoping somebody here could help me with. I have two classes, CMenu and CMenuItem, which are defined in their own header files "Menu.h" and "MenuItem.h" respectively. Now, the problem is that they both need to reference each other, as an instance of CMenu needs to have an array of CMenuItems and each CMenuItem needs to have an instance of CMenu. How do I setup my header file(s) so that I do not get errors because one doesn't know about the other????

    Code:
    // contents of file Menu.h
    #pragma once
    #include "MenuItem.h"
    
    class CMenu
    {
    private:
      CMenuItem* m_lpItems;
      int m_nItemCount;
    
    public:
      CMenu();
      virtual ~CMenu();
    };
    //-------------------------------------
    // contents of file MenuItem.h
    #pragma once
    #include "Menu.h"
    
    class CMenuItem
    {
    private:
      CMenu* m_lpSubMenu;
    
    public:
      CMenuItem();
      virtual ~CMenuItem();
    }
    I guess I could use a void pointer (void*) but that would seem to defeat the purpose. I'm sure there is a logical way to do this as I am sure there is no way I am the first person who has needed to do something like this...

    Any ideas??

    Thanks,
    Rippin

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Try this:
    Code:
    // contents of file Menu.h
    #pragma once
    #include "MenuItem.h"
    
    extern class CMenuItem;
    
    class CMenu
    {
    private:
      CMenuItem* m_lpItems;
      int m_nItemCount;
    
    public:
      CMenu();
      virtual ~CMenu();
    };
    //-------------------------------------
    // contents of file MenuItem.h
    #pragma once
    #include "Menu.h"
    
    extern class CMenu;
    
    class CMenuItem
    {
    private:
      CMenu* m_lpSubMenu;
    
    public:
      CMenuItem();
      virtual ~CMenuItem();
    }
    I think that should work. I'm not that certain about stuff like this, but I think that should work fine.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width