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

后端 未结 5 1866
长情又很酷
长情又很酷 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:26

    Stuff::things is only declared, but it is not defined.

    please use:

    // Stuff.cpp
    #include "Stuff.h"
    
    std::list Stuff::things;
    

    Added: it is also a good practice to protect your header files against multiple inclusion:

    // Stuff.h
    #ifndef STUFF_H_
    #define STUFF_H_
    
    #include 
    
    class Stuff {
        public:
           static std::list things;
    };
    
    #endif
    

提交回复
热议问题