0

I know that unresolved external symbol means that compiler can't find definition.

I got this error

Severity Code Description Project File Line Error LNK2001 unresolved external symbol "private: static class std::set,class std::allocator > VirtualWorld::vWords" (?vWords@VirtualWorld@@0V?$set@HU?$less@H@std@@V?$allocator@H@2@@std@@A) ConsoleApplication11 c:\Users\Laptop\documents\visual studio 2015\Projects\ConsoleApplication11\ConsoleApplication11\Source.obj 1

with this code below (this code is fragments, but it's still generates this error)

#include <set>  
#ifndef _TYPE_ID
#define _TYPE_ID
typedef unsigned int Id;
#endif

#ifndef _CLASS_VIRTUALWORLD
#define _CLASS_VIRTUALWORLD

class VirtualWorld
{
private:
    static Id freeCounter;
    static std::set<Id> vWorlds;
    bool holding;
    Id virtualWorldId;

    static Id hold();
    static void release(const Id & i);
public:
    VirtualWorld();
    ~VirtualWorld();

    Id getInstance();
    void forceRelease();
};
#endif 

int main() { 
    VirtualWorld virWorld;
    return 0;
}

Id VirtualWorld::freeCounter = 1;

Id VirtualWorld::hold() {
    std::set<Id>::iterator iter = vWorlds.lower_bound(freeCounter);
    while (iter != vWorlds.end() && *iter == freeCounter)
    {
        ++iter;
        ++freeCounter;
    }
    return freeCounter++;
}

void VirtualWorld::release(const Id & i) {
    vWorlds.erase(i);
    freeCounter = 1;
}

VirtualWorld::VirtualWorld() : holding(false) { }

VirtualWorld::~VirtualWorld()
{
    if (holding) {
        release(virtualWorldId);
    }
}

Id VirtualWorld::getInstance() {
    if (!holding) {
        virtualWorldId = hold();
        holding = true;
    }
    return virtualWorldId;
}

void VirtualWorld::forceRelease() {
    if (holding) {
        release(virtualWorldId);
        holding = false;
    }
}

I researched that I'm getting this error then trying to access vWorlds in hold() and release(int) functions. Why I'm getting this error and what should I change ?

Vinigas
  • 464
  • 5
  • 18
  • 1
    You forgot to *define* `VirtualWorld::vWorlds`, you have only *declared* it. – Some programmer dude Jan 25 '16 at 13:32
  • I want `vWorlds` to be empty std::set at the beginning of the program. If I written `static std::set vWorlds;` shouldn't it create std::set with default constructor already ? – Vinigas Jan 25 '16 at 13:40
  • No that's just the declaration, you need to actually define the variable as well, which you do by "redeclaring" it in the global scope. That is what causes the default construction of the (empty) set. So do just what you do for `VirtualWorld::freeCounter` but without any explicit initialization. – Some programmer dude Jan 25 '16 at 13:43

0 Answers0