【发布时间】:2025-12-05 13:35:01
【问题描述】:
我正在检索存储在 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);
}
}
有人知道我做错了什么吗?
【问题讨论】:
-
搜索“c++ 静态成员未定义引用”之类的术语应该会出现*.com/questions/35565971/… 之类的内容,表明您需要将
std::vector<std::string> objectFactory::objList;放入您的cpp 文件中。
标签: c++ json vector static undefined-reference