static vector<string> member;

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Rockair
    New Member
    • Jul 2007
    • 1

    static vector<string> member;

    hi!

    there is a class:

    Code:
    class card
    {
    static vector<string> names;
    //...
    };
    
    //i tried to initialize this way:
    
    vector<string> names.push_back("Two of Diamonds");
    //but it gives this error:
    //error C2143: syntax error : missing ';' before '.'

    //i tried to init in the ctor, but that is gave another error, a link error:


    //Error : unresolved external symbol "public: static class std::vector<cla ss std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> >,class std::allocator< class std::basic_stri ng<char,struct std::char_trait s<char>,class std::allocator< char> > > > card::names" (?names@card@@2 V?$vector@V?$ba sic_string@DU?$ char_traits@D@s td@@V?$allocato r@D@2@@std@@V?$ allocator@V?$ba sic_string@DU?$ char_traits@D@s td@@V?$allocato r@D@2@@std@@@2@ @std@@A)

    i have no idea how could i initialize the names variable with several stings
    or... is it impossible?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Declaring a static class member does not create the class member.

    If it did, every time you incuded you class definition in your code you would get a new static class member which would name collide with the old one.

    You have to define (int the class you obnly declare) your static member. Thsat, you have to create it yourself:


    vector<string> card::names;

    int main()
    {

    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Declaring a static class member does not create the class member.

      If it did, every time you incuded you class definition in your code you would get a new static class member which would name collide with the old one.

      You have to define your static member (in the class you only declare it) . That, you have to create it yourself:

      [code=cpp]
      vector<string> card::names;

      int main()
      {
      card c;
      }
      [/code]

      The larger issue is why is this thing static???

      Comment

      Working...