Global Class Objects?

closed account (jw6XoG1T)
Im making a game engine right now and i need to make an object called "engine" from the Engine class available to all the other classes i made in other source files. How would i go about making a "global object"?

I know that variables have to be declared as:

header file 1: extern int a;
source file 1: int a = 1;

is there a similar method to make a class object available to everything within the program? Right now i have my object declared as so:

Engine engine;

How would i make "engine" global so that i could use it within other classes in different source files?
By declaring it in a header as:
extern Engine engine;

When there should be at most one instance of that class, the singleton pattern is often used.
closed account (jw6XoG1T)
i dont need to redeclare it again in my .cpp file? Like this:

Header:
extern Engine engine;

Cpp File:
Engine engine;
You do.

extern Engine engine; is just a declaration, so you need to instantiate the engine object somewhere.
closed account (jw6XoG1T)
hold on lemme try that then...i think it didnt work before though :p
closed account (jw6XoG1T)
it doesnt work :( here's what it says:




1>------ Build started: Project: DarkMatter Engine, Configuration: Debug Win32 ------
1>  WinMain.cpp
1>c:\users\danielle\documents\visual studio 2010\projects\darkmatter engine\darkmatter engine\winmain.h(5): error C2653: 'DarkMatter' : is not a class or namespace name
1>c:\users\danielle\documents\visual studio 2010\projects\darkmatter engine\darkmatter engine\winmain.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\danielle\documents\visual studio 2010\projects\darkmatter engine\darkmatter engine\winmain.h(5): warning C4091: 'extern ' : ignored on left of 'DarkMatter::Engine' when no variable is declared
1>  Engine.cpp
1>c:\users\danielle\documents\visual studio 2010\projects\darkmatter engine\darkmatter engine\winmain.h(5): error C2653: 'DarkMatter' : is not a class or namespace name
1>c:\users\danielle\documents\visual studio 2010\projects\darkmatter engine\darkmatter engine\winmain.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>  Generating Code...


Erm, this looks like it is inside a class or something. Post your code, please.
Looks like your class isn't declared in your header, so include the appropriate file.
closed account (jw6XoG1T)
I did declare it in my header though :( hold on ill post the code
closed account (jw6XoG1T)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

#ifndef __ENGINE_H
#define __ENGINE_H

#include <Windows.h>
#include <iostream>
#include <d3d9.h>
#include <d3dx9.h>
#include "Sprite.h"

using namespace std;

const int VERSIONMAJOR = 1;
const int VERSIONMINOR = 0;
const int VERSIONREVISION = 0;

extern bool gameover;
extern const int SCREENH;
extern const int SCREENW;
extern const string Title;

extern HWND g_window;
extern HINSTANCE g_instance;
extern LPSTR g_CmdLine;
extern int g_CmdShow;

namespace DarkMatter
{
	class Engine
	{

	private:

		int e_versionmajor, e_versionminor, e_versionrevision;
		HWND window;
		LPDIRECT3D9 direct;
		LPDIRECT3DDEVICE9 device;
		LPDIRECT3DSURFACE9 backbuffer;
		LPD3DXSPRITE sprite;

		LPDIRECT3DTEXTURE9 character;

	public:

		Engine();
		~Engine();


		bool e_Init();
		void Render();
		void Message(string message, string title);

		//Access/Mutator functions
		void SetWindow(HWND g_window) {this -> window = g_window;}
		HWND GetWindow() {return this -> window;}
		string GetVersion();

	};
};


#endif __ENGINE_H



1
2
3
4
5
6
7
8
9
10
11
12
13
14

#ifndef __WINMAIN_H
#define __WINMAIN_H

#include <iostream>
#include <Windows.h>
#include "Engine.h"

extern DarkMatter::Engine engine;

#endif __WINMAIN_H





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86


#ifndef __WINMAIN_H
#define __WINMAIN_H

#include "WinMain.h"
#include "Engine.h"

using namespace DarkMatter;

DarkMatter::Engine engine;

LRESULT CALLBACK WinProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_DESTROY:
		{
			gameover = true;
			PostQuitMessage(0);
		}
	}

	return DefWindowProc(window, message, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow)
{
	g_instance = instance;
	g_CmdLine = lpCmdLine;
	g_CmdShow = nCmdShow;

	WNDCLASSEX wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = NULL;
	wc.hIconSm = NULL;
	wc.hInstance = instance;
	wc.lpfnWndProc = (WNDPROC)WinProc;
	wc.lpszClassName = Title.c_str();
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW | CS_HREDRAW;
	RegisterClassEx(&wc);

	g_window = CreateWindowEx(
		0,
		Title.c_str(),
		Title.c_str(),
		WS_OVERLAPPEDWINDOW,
		0, 0,
		SCREENW, SCREENH,
		NULL,
		NULL,
		g_instance,
		NULL);

	ShowWindow(g_window, g_CmdShow);
	UpdateWindow(g_window);

	MSG message;

	engine.SetWindow(g_window);
	engine.e_Init();
	engine.Message(engine.GetVersion(), "DM Version");

	while (!gameover)
	{
		if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&message);
			DispatchMessage(&message);
		}

		engine.Render();
	}

	return message.wParam;
}


#endif __WINMAIN_H

I am very new to C++ so I may be very far off, but you have this:
1
2
#ifndef __WINMAIN_H
#define __WINMAIN_H 


...in the second file where you declare your object. You also have that in the third file when you try to instantiate it. If the third file gets processed first, would the second file (containing the declaration) be skipped?
closed account (jw6XoG1T)
the third file include the second file through this:

#include "WinMain.h"

because of that, the include files get processed first and then the rest of the program executes...this means the second file is processed before the third. Make sense?
Make sense?
Not really. Why would you put header guards in a cpp file ? Do remove them..
Topic archived. No new replies allowed.