0

I know this is similar to a bunch of other undefined static reference questions, but in this one I can see that the "undefined" static variable is defined.

I have several errors like this. Here's a sample of my code:

gmcharacter.h:

const string GMCHARACTER("character");
class GameCharacter : public GamePart
{
    private:
        string name;
        static vector<GameCharacter*> characterList;
    public:
        GameCharacter(cstring nm, cstring id) :
            GamePart(GMCHARACTER, id, TRUE, TRUE, TRUE),
            name(nm)
            { }
...
}

gamecharacter.cpp

vector<GameCharacter*> characterList;
...

make output:

g++  -DYYERROR_VERBOSE -Wall -std=c++11 -g -MT gamecharacter.o -MMD -MP -MF .d/gamecharacter.Td -save-temps  **-c -o gamecharacter.o gamecharacter.cpp**
g++ -o ../mkscript -lstdc++ -g gamepart.o **gamecharacter.o** gamecondition.o gamedoor.o gameroom.o gamething.o gamedot.o gamemap.o gameoption.o condflag.o game.o flaglist.o IDList.o imagedesc.o lexer.o mkscript.o util.o commandargs.o PHPWriter.o ASWriter.o PHPArgumentList.o ASArgumentList.o gh1.tab.o
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: gamecharacter.o:gamecharacter.cpp:(.rdata$.refptr._ZN13GameCharacter13characterListE[.refptr._ZN13GameCharacter13characterListE]+0x0): **undefined reference to `GameCharacter::characterList'**
A. P. Damien
  • 376
  • 2
  • 10
  • 2
    `vector GameCharacter::characterList;`. You've defined a global member variable, not the static class member. – 1201ProgramAlarm May 31 '20 at 01:30
  • The emphasis in "I _know_ I have defined" pretty much guarantees that it was not-quite defined. :) – JaMiT May 31 '20 at 03:45
  • Thanks to 1201ProgramAlarm for actually answering the question. I have added an answer for this case to: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix to help the next person who makes this mistake. – A. P. Damien May 31 '20 at 06:08
  • @A.P.Damien Be aware that an answer for this case had already been present in [this answer](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574407#12574407) under the heading "A common mistake is forgetting to qualify the name" (equally applicable to member functions and static member variables). *That doesn't invalidate your answer over there. It just seemed as though you thought this case was not previously covered.* – JaMiT Jun 01 '20 at 01:13

0 Answers0