CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2008
    Posts
    3

    Exclamation Using 1 Class in Multiple Files

    I'm trying to create a class inside a header file so that all my files can access that 1 class and only use that one. This is what I have so far.

    ----------

    Window.h:

    class Window
    {
    public:
    Window();
    ~Window();

    /* FUNCTIONS */
    HWND OpenWindow(LPCTSTR, int, int);

    /* VARIABLES */
    int IsRunning;
    };

    typedef class Window *WINDOW;

    ----------

    Main.h:

    class Main
    {
    public:
    Main();
    ~Main();

    WINDOW GameWindow;
    };

    Main::Main()
    {
    Main::GameWindow = new Window();
    }

    typedef class Main *MAIN;

    ----------

    The above code works and lets me call Main::GameWindow in every file but that's as far as I can go. I can't call Main::GameWindow->OpenWindow(). How do I fix this?
    Last edited by Driklyn; May 25th, 2008 at 10:53 PM.

  2. #2
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    Re: Using 1 Class in Multiple Files

    Are you including the proper headers?
    Forward declarations may help.

    When you put
    Code:
    Main::GameWindow->OpenWindow()
    Or is 'Main' just a placeholder for a class object of type Main?

    I'm not understanding the problem here. Do you get an error?
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  3. #3
    Join Date
    May 2008
    Posts
    3

    Re: Using 1 Class in Multiple Files

    If I try to use Main::GameWindow->OpenWindow() it gives me an error, but Main::GameWindow works. It's like it can see the functions inside of GameWindow for some reason. This guy on this other forum suggested I use "static WINDOW GameWindow" in Main.h but when I do that I get this error:

    error LNK2001: unresolved external symbol "public: static class Window * Main::GameWindow" (?GameWindow@Main@@2PAVWindow@@A)

  4. #4
    Join Date
    May 2008
    Posts
    3

    Re: Using 1 Class in Multiple Files

    Nevermind, I solved it. I was trying to initialize GameWindow inside the constructor of Main() and this was causing that error. I just had to move it outside the constructor and it worked...

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

Featured