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:


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

namespace tnt
	class Engine
		class GeneralCore
			class WindowManager
				sf::RenderWindow MainWindow;
				int windowHeight;
				int windowWidth;
				int windowBPP;
				std::string windowTitle;
				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();



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;

	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

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

Edited by epicasian: n/a

6 Years
Discussion Span
Last Post by epicasian

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:

#include "Cores.h"

class Engine
   class GeneralCore;
   class GraphicsCore;
   class GameCore;
   class LogicCore;

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

Edited by epicasian: n/a


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.

Edited by mike_2000_17: n/a


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

Thanks again,

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.