0

I have been stuck a few days now on this issue, and I cannot find an answer to my problem even though google is fuul of replies it seems like this is quite an abstract problematic.

Here is my code in H:

struct DISPLAYLINE_t {
         char  *text;
         bool isWhite;
         void set(char *txt, bool iswhite){text = txt; isWhite = iswhite;};
};

struct DISPLAY {   
    static DISPLAYLINE_t line1,line2,line3,line4; 
    void clear(){//dostuff};
};

When I try to access from my Main:

DISPLAY::line1.set(string, FALSE);

I get the following error:

error LNK2019: unresolved external symbol "public: static struct DISPLAYLINE_t DISPLAY::line1" (?line1@DISPLAY@@2UDISPLAYLINE_t@@A) referenced in function WinMain

Any ideas?

Jmorvan
  • 1,020
  • 11
  • 31
  • is the class public ? – Nathan Apr 04 '13 at 13:34
  • possible duplicate of ["Undefined reference" trying to reference an static field](http://stackoverflow.com/questions/9446365/undefined-reference-trying-to-reference-an-static-field) – Bo Persson Apr 04 '13 at 13:36

1 Answers1

3

You have to provide a definition at global namespace scope for your static data members (at least for those that you are odr-using in your code):

DISPLAYLINE_t DISPLAY::line1;
DISPLAYLINE_t DISPLAY::line2;
DISPLAYLINE_t DISPLAY::line3;
DISPLAYLINE_t DISPLAY::line4;

This live example shows how you should fix your program.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Cheers man you rock! out of curiosity... what do you mean by "odr-using"? – Jmorvan Apr 04 '13 at 13:47
  • what do you mean by "odr-using"? – Jmorvan Apr 04 '13 at 13:49
  • 1
    @Jmorvan: That's a technical terms, and it is defined in the C++11 Standard, Paragraph 3.2: "*A variable or non-overloaded function whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) is immediately applied.*". In other words, since the name of `line1` appears in a potentially evaluated expression (and your data member is not `constexpr`), you are odr-using it – Andy Prowl Apr 04 '13 at 13:53