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?

You had

#include "Window.h"

at the top of Main.h, right? You'll also want sentinels for your header files, so they don't get included twice:

Window.h

#ifndef WINDOW_H
#define WINDOW_H

// code goes here

#endif

Main.h

#ifndef MAIN_H
#define MAIN_H

// etc

#endif

and so on. Aside from that, perhaps elaborating on "I can't call it" might help a bit.

I have those in there, I just didn't post them. And by "call it", I mean I want to be able to do something like Main::GameWindow->OpenWindow() in any file I choose.

Basically, I don't want to have to create a new window class in every file. I want to define it once, like in Main.h:

WINDOW GameWindow;

And then be able to call any functions associated with GameWindow with any .cpp file I have.

So you want Main::GameWindow to be accessible from any function, without needing any instance of 'Main' to be present? Try declaring GameWindow as static.

I did that but it gives me this error:

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

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...

This article has been dead for over six months. Start a new discussion instead.