1

I'm coding a game engine and I'm trying to make all it's functions sorta hidden and mostly rule-free so when programmers code the game, they don't bother with how or why something needs to be that way or another, and I'm trying to stick to leave it purely as C++ language.

So, for example, I don't want them to:

//Going to create a new object on screen
Object* newObject = gameEngine.gameModels.NewObject(); //I don't want this

Instead, I want them to be able to do this:

//Going to create a new object on screen
Object* newObject = new Object(); 
newObject->Load("objectName"); //Done!

But for this to work, I need to keep the address of my resource loader so I can call the proper Load function and do the actual loading, and this is what I'm doing:

class BaseModel
{
    friend GameEngine;
    private:
        static ResourceModule* moduleAddress;
};
class Model : public BaseModel
{
    public:
        void Load(char* assetName) 
        {
            //use moduleAddress as needed 
        };
};
class GameEngine
{
    public:
        void Initialize()
        {
            myBaseModel.moduleAddress = &myResourceModule;
        }
    private:
        ResourceModule myResourceModule;
        BaseModel myBaseModel;
};

But I'm getting an unresolved external error for the "static ResourceModule* resourceModuleAddress" line, and I can't get the "friend" keyword to work (it also says the variable is private and can't be accessed).

Any suggestions on how can I accomplish what I'm trying to do, or how can I get this code above working?

Danicco
  • 1,573
  • 2
  • 23
  • 49
  • It seems like you are trying to implement *reflection* for C++. Search for this keyword, I am sure you will have plenty of solutions. – amit Aug 04 '13 at 22:34
  • Have you forward-declared `GameEngine` before the `friend`-declaration in `BaseModel`? – dyp Aug 04 '13 at 22:40
  • Typos fixed, and it worked after I added the GameEngine declaration above it! Thanks! – Danicco Aug 04 '13 at 22:44

1 Answers1

4

Unresolved external error comes from lack of definition of the static member. Solution here: Initializing private static members

Community
  • 1
  • 1
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • 1
    *A bit pedantic*: They come from lack of *definition*, not lack of *initialization*. – dyp Aug 04 '13 at 22:36