0

First things first, I think it will make more sense to see my code. Header:

#include <vector>
#include "GUIItem.h"

class WindowManager
{
private:
    static WindowManager* s_wndmgr; //A singleton maintains a pointer to itself as a class variable
    std::vector<GUIItem*> m_guilist; //storage for gui item
//...

public:
    static void Create();
    static void Destroy();
    static inline WindowManager* Get()
    {
        return s_wndmgr;
    }
    static void addItem(GUIItem *newGUIItem);
};

And the class:

#include "WindowManager.h"
#include "GUIButton.h"

WindowManager* WindowManager::s_wndmgr = NULL;

WindowManager::WindowManager()
{
    s_wndmgr = NULL;
}

WindowManager::~WindowManager()
{
    //Cleanup other stuff if necessary

    delete s_wndmgr;
}

void WindowManager::Create()
{
    if ( !s_wndmgr ) s_wndmgr = new WindowManager();
    GUIButton *m_btn1 = new GUIButton();
    addItem(m_btn1);    
}

void WindowManager::Destroy()
{
    if ( s_wndmgr ) delete s_wndmgr;
}

void WindowManager::addItem(GUIItem * newGUIItem)
{
    m_guilist.push_back(newGUIItem);
}

Hopefully it makes some kind of sense. I'm trying to create a simple gui framework from scratch in OpenGL and this is a simple window manager. My issue is with m_guilist which should be accessible so that new GUIItems can be added to it such as happens in Create (GUIItem being a base class from which others inherit, such as GUIButton).

In this case I'm using addItem in order to append items to the list but I'm running into the a nonstatic member reference must be relative to a specific object error regarding the line inside addItem. I'm a little confused as to why this is the case. I understand that making addItem static is the reason for this error, but that was done in order for it to be called from within Create. Is there a way around this?

Sorry, this is quite the poor question and my grasp of C++ isn't great yet though I'm getting there. Any thoughts on this? Something tells me I'd be better to leave the Create function alone and create another nonstatic function to create my GUIItems and add them to the list.

LogicStuff
  • 19,397
  • 6
  • 54
  • 74
Hexodus
  • 369
  • 6
  • 18

1 Answers1

1

addItem is a static function, which does not not operate on any instance of WindowManager. It can not access m_guilist, which is non-static without an instance.

Maybe you just want:

Get()->m_guilist.push_back(newGUIItem);

But you're starting to make the interface static, that's kind of hybrid. It's usually that addItem is non-static and you call it with the instance you acquire by WindowManager::Get().

Yet, WindowManager doesn't have inaccessible or deleted constructor to qualify as a singleton class. Ways to implement a Singleton design pattern.

Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74
  • lol he's not going to able to understand this. @Jamie4840 Just add "static" to your m_guilist variable – Seth Kitchen Dec 11 '15 at 22:33
  • I do understand this thanks. The only reason `addItem` is static is otherwise it can't be accessed by `Create`, there'll be a similar error. I have tried making `m_guilist` static but I get an error about an unresolved external symbol relating to `static std::vector` – Hexodus Dec 11 '15 at 22:36