简体   繁体   中英

Public Static Variable access

I'm tring to expose a static variable. I have tried doing this as both just a public static, and using access functions, but when I use the command Stage::SetFramework( this ); in my Framework class, or even if I make systemFramework public and use Stage::systemFramework = this , I get:

framework.obj||error LNK2001: unresolved external symbol "public: static class Framework * Stage::systemFramework" (?systemFramework@Stage@@2PAVFramework@@A)|

I'm not sure why this isn't working. I'm obviously missing something Can anyone help please?

#pragma once

#include "event.h"

#ifndef Framework
class Framework;
#endif // Framework

class Stage
{
  protected:
    static Framework* systemFramework;

  public:
    // static Framework* systemFramework;

    // Stage control
    virtual void Begin() = 0;
    virtual void Pause() = 0;
    virtual void Resume() = 0;
    virtual void Finish() = 0;

    virtual void Event(FwEvent *e) = 0;

    virtual void Update() = 0;
    virtual void Render() = 0;

    static void SetFramework( Framework* FrameworkObject )
    {
      systemFramework = FrameworkObject;
    };
/*
    static Framework* GetFramework()
    {
      return systemFramework;
    };
*/
};

Thanks

Listing static class data members in a class only declares them. They must still be defined somewhere. Put this definition into one .cpp file:

Framework *Stage::systemFramework;

That's because you need a FrameWork* Stage::systemFramework; somewhere too (in a .cpp file, typically). This "places" your variable you may, for example for caching reasons, have it next to some othver variables - so the compiler won't just throw it anywhere, so the declaration inside the class definition is just that, a declaration that "there will be one of these variable somewhere". [Or in an embedded system, there may be some part of memory that is backed up by battery power and another part of memory that isn't, and again, where you "place" the variable will matter in this case].

Of course, the public , private or protected inside the class will still determine which parts of the code can access the variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM