简体   繁体   中英

C++ Undefined reference to Holder::menus (Struct)

Ok, so I have a struct defined as thus:

#ifndef __STRUCTS_H__
#define __STRUCTS_H__

struct counts {
    int views = 0;
    int inits = 0;
};

#endif

I have a class that is going to have entirely static methods and variables that are accesable by all classes.

#ifndef __HOLDER_H__
#define __HOLDER_H__

#include "Structs.h"

class Holder
{
public:
    static counts menus;

    Holder() {
        menus = counts();
    }
};

#endif

And so I tried to acess this method and the compiler spits out the error "Undefined reference to Holder::menus"

Here is the segment that triggers this (HelloWorldScene.cpp)

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "Holder.h"
#include "Structs.h"

USING_NS_CC;

HelloWorld::HelloWorld(void)
{
    //Constructor
    Debug::crashLog("**__Menu Deinit__**");
    //SUDO Missing stuff
    Holder::menus.inits -= 1;
}

Why is it having issues?

in your Holder implementation file you need this:

counts Holder::menus;

if you don't have Holder.cpp file, (and you don't want one) you can put it directly into in HelloWorldScene.cpp .

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