【问题标题】:How to add object in the static list on the constructor [duplicate]如何在构造函数的静态列表中添加对象[重复]
【发布时间】:2018-04-05 08:58:58
【问题描述】:
#include <forward_list>

using namespace std; 

class Test {
public:
    Test(){objects.push_front(this);}
private:
    static forward_list<Test*>objects; 
};

int main(){
    Test a; 
}//Visual Studio 17, error

Visual Studio 没有说明问题所在。它只是重新运行这两个代码 - LNK1120 和 LNK2001。

【问题讨论】:

    标签: c++ static visual-studio-2017


    【解决方案1】:

    你有一个未定义的 static forward_list&lt;Test*&gt;objects; 引用你必须像这样定义你的 static 对象:

    #include <forward_list>
    
    using namespace std; 
    
    class Test {
    public:
        Test(){objects.push_front(this);}
    private:
        static forward_list<Test*>objects; 
    };
    
    forward_list<Test*> Test::objects;
    
    int main(){
        Test a; 
    }//Visual Studio 17, error
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-26
      • 2018-03-19
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多