Hello, I'm trying to create my own game engine, and ran into a problem with nested classes.

Currently, I only have one class that has any methods:

#ifndef WINDOWMANAGER_H_
#define WINDOWMANAGER_H_

#include <SFML/Graphics.hpp>
#include <string>

namespace tnt
{
	class Engine
	{
	public:
		class GeneralCore
		{
		public:
			class WindowManager
			{
			private:
				sf::RenderWindow MainWindow;
				int windowHeight;
				int windowWidth;
				int windowBPP;
				std::string windowTitle;
			public:
				WindowManager();
				WindowManager(int width, int height, int bpp, std::string title);
				void ChangeWindowSize(int newWidth, int newHeight);
				void ChangeWindowTitle(std::string newTitle);
				void SwapBuffer();
				void DisplayWindow();

			};
		};
	};

}
#endif

Am I not nesting the classes right? It compiles fine on MSVC++ 2008.

When I try to access the window manager instance from main like this:

#include "TNTengine.h"
using namespace tnt;

int main(int argc, char* args[]) 
{ 
	Engine myEngine;
	bool running = true;
	while(running)
		myEngine.GeneralCore.WindowManager.DisplayWindow();

	return 0; 
}

MSVC++ gives compiler errors like "left of '.WindowManager' must have class/struct/union"

Am I not setting up the classes right? Or is it something else?

Thanks in advance
~Asian

EDIT: I want to declare the other classes else where. Thanks again

Recommended Answers

All 5 Replies

Your syntax is wrong. You need to use the scope resolution operator.
So your call would look like this myEngine::GeneralCore::WindowManager:: DisplayWindow()

It looks a bit to me, from your class names, that defining your own namespaces might be a good option for you to consider instead of nested classes.. just a suggestion.

I tried the scope resolution operator, but that didn't seem to work either. I would like to use namespaces for all my would be classes, but when I'm done with the engine, I want to be able to create an instance of "Engine" so all subsystems will be created as well; instead of instantiating every single subsystem, one by one.

Is there a way to create one class out of many different classes?

Like this:
(pseudocode)

#include "Cores.h"

class Engine
{
   class GeneralCore;
   class GraphicsCore;
   class GameCore;
   class LogicCore;
};

Thanks for all the help, it is VERY much appreciated! :)

Consider something like this:

#include "Cores.h" //I assume this would contain all GeneralCore, GraphicsCore.. but I would suggest you have one file per core, in fact one header per class in general.

namespace my_engine {

class Engine
{
   GeneralCore mMyGeneralCore;
   GraphicsCore mMyGraphicsCore;
   GameCore mMyGameCore;
   LogicCore mMyLogicCore;
};

};

// in GeneralCore.hpp
namespace my_engine {

class GeneralCore {
  ... whatever ...
};

};

// so on for the others cores.

With proper implementation of all the cores and the Engine class, all you would have to do to start the game would be:

#include "Engine.hpp"

using namespace my_engine;

int main() {
  Engine MyGame(.. some parameters like which game logic to start ..);

  MyGame.run(); // or something like that.
};

I have always done something like that for my past game engines.

In response to:
>>Is there a way to create one class out of many different classes?
Well it's a bit vague. Consider this discussion. That gives a few guidelines in general with respect to multiple inheritance (I would not recommend that, but the discussion there also discusses all the alternatives which are probably better suited for your application). You should rely a lot on polymorphism, abstraction and encapsulation. Make sure to understand those topics before making too many structural decisions about your engine.

Thank you very much, Mike. That is exactly what I was looking for!

Thanks again,
~Asian

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.