2

I am trying to map json data to a class called Game by using the macro NLOHMANN_DEFINE_TYPE_INTRUSIVE but I am getting the error:

error: no matching function for call to ‘nlohmann::basic_json<>::get<g::Game>()’ 19 | game = data.get<g::Game>();

This is the class:

namespace g{
class Game {
public:
    
    NLOHMANN_DEFINE_TYPE_INTRUSIVE(Game, _name, audience)
private:
    uintptr_t _id; // unique id can act as an invitation code
   
    uintptr_t _ownerID;
    bool _started;
    std::vector<Connection> _players;

  std::string _name;
    //Bounds of player given in json file
    struct PlayerCount {
        int min;
        int max;
        NLOHMANN_DEFINE_TYPE_INTRUSIVE(PlayerCount, min, max)
    } player_count;
    bool audience;

    // map of { name_string -> { configurable_value , prompt_text } }
    std::map<std::string, std::pair<std::any, std::string>> setup;

    std::map<std::string, List> constants;
    std::map<std::string, List> variables;
    std::map<std::string, List> per_player;
    std::map<std::string, List> per_audience;
    std::vector<Rule> rules;
};
}

The error is coming from here, specifically the get<g::Game>() line:

using json = nlohmann::json;
using namespace g;

InterpretJson::InterpretJson(string path){
    this->path = path;
    ifstream f(path);
    json jData = json::parse(f);
    f.close();
    data = jData;
}


void InterpretJson::interpret(Game& game){
   game = data.get<g::Game>(); //data is private member defined in InterpretJson.h and is of type json
}

How do I fix this?

wohlstad
  • 12,661
  • 10
  • 26
  • 39
callum arul
  • 159
  • 6
  • 1
    Cannot reproduce: https://godbolt.org/z/rfbesMfrT Please make a minimal, reproducible example https://stackoverflow.com/help/minimal-reproducible-example – Frodyne Oct 19 '22 at 07:02

1 Answers1

0

Your issue is, that your hiding the struct from nlohmann entirely. If you declare

struct PlayerCount {
    int min;
    int max;
    NLOHMANN_DEFINE_TYPE_INTRUSIVE(PlayerCount, min, max)
} player_count;

outside of your class in the namespace g the macro would work. I would suggest you, if you want to hide the struct from outside, to just add a static method inside.