1
votes

I'm not having a lot of luck in C++ getting one of my classes to see/reference/copy data from one of my other classes so it can use it.

Basically I get the error 'Core' does not name a type or when I try to forward declare (http://stackoverflow.com/questions/2133250/does-not-name-a-type-error-in-c) I get field 'core' has incomplete type

I'm guessing the second error is due to the class not really being initialized possibly, so it has nothing to get? I dunno :( (see code at the bottom)

In my C# games I would normally create a "core" class, and then within that I would start other classes such as 'entities', 'player', 'weapons', etc. When I start these other classes I would pass "this"

public WeaponManager c_WeaponManager;
...
c_WeaponManager = new WeaponManager(this);

so I could always access public values of any class from anywhere as long as it passed through core. Eg: So when I do my update through the 'weapon' class, and it detects its hit the player, I'd simply get a function within that class to...

core.playerClass.deductHealth(damageAmmount);

..or something like that.

It allowed me to keep lots of variables I wanted to access globally neatly tucked away in areas that I felt were appropriate.

I know this isn't a good method of programming, but its what I'm fairly comfortable with and I mainly do hobby programming so I like being able to access my data quickly without bureaucratic Get() and Set() functions handing data from one class to another and another. Also I'm still fumbling my way through header files as they seem to be a pain in the ass

//-----------[In vid_glinit.h]-------------------
include "core.h"

class Vid_glInit
{
    public:
    RECT glWindowRect;
    Core core;

    Vid_glInit();
    ~Vid_glInit();
    void StartGl(HWND _hGameWindow, int resolutionX, int resolutionY);

    private:
};

//------------[in core.h]----------

include "vid_glinit.h"

class Core
{
    public:
    Vid_glInit vid_glinit(this);

    enum GAME_MODE
    {
        INIT,
        MENUS,
        GAMEPLAY
    };

    GAME_MODE gameMode;
    HWND hGameWindow;
    HGLRC hGameRenderContext;                           // Permanent Rendering Context
    HDC hGameDeviceContext;                         // Private GDI Device Context

    //functions go here
    Core();
    ~Core();
    void testFunc();
    void Run();
    void Update();
    void Render();
    void StartGl(int resoultionX, int resolutionY);

    private:

};

The goal is that when I start OpenGl, instead of having lots of little functions to pass data around I simply tell the glFunctions who need the Device or Rendering context to use core.hGameDeviceContext , if that makes sense

1
You might want to tag this as C++ rather than C#.Jim Mischel

1 Answers

1
votes

The problem is that you've got

class Vid_glInit
{
public:
    Core core;

which means allocate a full copy of the Core object inline inside this class, and also

class Core
{
public:
    Vid_glInit vid_glinit(this);

which means allocate a full copy of the Vid_glInit object inline inside the class - and this is now circular, and neither structure's size can be computed.

You probably actually want to allocate at least one of them by reference or pointer, i.e.

class Core
{
public:
    Vid_glInit* vid_glinit; // pointer: access properties as core.vid_glinit->foo
    Vid_glInit& vid_glinit; // reference: properties are core.vid_glinit.foo

In that case you can use the class Vid_glInit; simple forward declaration because these are just pointers internally and the size of a pointer is fixed regardless of the structure behind it, i.e. C++ can lay out the Core class in memory without full knowledge of the Vid_glInit structure.