Why won't my C++ program link when my class has static members?

后端 未结 5 1864
长情又很酷
长情又很酷 2021-01-18 04:55

I have a little class called Stuff that I want to store things in. These things are a list of type int. Throughout my code in whatever classes I use I want to be able to a

5条回答
  •  被撕碎了的回忆
    2021-01-18 05:10

    Mentioning a static member in a class declaration is a declaration only. You must include one definition of the static member for the linker to hook everything up properly. Normally you would include something like the following in a Stuff.cpp file:

    #include "Stuff.h"
    
    list Stuff::things;
    

    Be sure to include Stuff.cpp in your program along with Main.cpp.

提交回复
热议问题