如何定义类的静态成员

时间:2020-09-18 18:48:50

标签: c++ json vector static undefined-reference

我正在检索存储在json文件中的路径列表,我没有收到路径的麻烦,但是遇到了错误undefined reference to `objectFactory::objList[abi:cxx11]',当我尝试将路径添加到相同路径的静态向量中时课。

我试图更改两者的类型,以查看是否正在寻找一个名称相同但类型不同的变量,但这还是个问题。

我还尝试将向量设置为最终列表的另一个向量,但这并不能解决问题。

objectFactory.h:


#ifndef PROJECT2DTD_OBJECTFACTORY_H
#define PROJECT2DTD_OBJECTFACTORY_H
#include <vector>
#include <string>
#include <fstream>
#include <nlohmann/json.hpp>

class objectFactory {
public:
    static void genObjList();
private:
    static std::vector<std::string> objList;
};

#endif //PROJECT2DTD_OBJECTFACTORY_H

objectFactory.cpp

#include <iostream>
#include "objectFactory.h"
using json = nlohmann::json;

void objectFactory::genObjList() {
    auto file = json::parse(std::fstream("assets/objects/objectList.json"))["objects"];//[0]["path"]
    for(auto i = 0; i != file.size(); ++i) {
        auto path = file[i]["path"].get<std::string>();
        objList.emplace_back(path);
    }
}

有人知道我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

您需要定义该数据成员。例如,在您的cpp文件顶部:

std::vector<std::string> objectFactory::objList;