0

I'm retrieve a list of paths stored in a json file, I'm not having trouble receiving the path but I get the error undefined reference to `objectFactory::objList[abi:cxx11]' When i try to add the path to a static vector of the same class.

I tried changing the types of both to see if maybe it was an issue where it was looking for a variable with the same name but a different type, but alas the issue remained.

I also tried to set the vector to be another vector of the finalized list but that didn't solve the issue.

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);
    }
}

Does anyone know what I might be doing wrong?

Kodi_x86
  • 3
  • 1
  • Searching for terms like "c++ static member undefined reference" should turn up stuff like https://stackoverflow.com/questions/35565971/undefined-reference-to-declared-c-static-member-variable that indicate you need to put `std::vector objectFactory::objList;` in your cpp file. – TheUndeadFish Sep 18 '20 at 18:53

1 Answers1

0

You need to define that data member. For example, at the top of your cpp file:

std::vector<std::string> objectFactory::objList;
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27