Reputation: 610
I'm trying to build a simple catch structure but I got below error related to nlohmann json library.
error: ‘exception’ in ‘using json = class nlohmann::basic_json<> {aka class nlohmann::basic_json<>}’ does not name a type
catch (nlohmann::json::exception& e)
here is the code;
#include <iostream>
#include <nlohmann/json.hpp>
namespace company
{
namespace project
{
class Serializable
{
public:
virtual void read(const nlohmann::json& json) = 0;
virtual void write(nlohmann::json& json) const = 0;
bool load(const std::string& fileName)
{
try
{
nlohmann::json json;
std::ifstream f(fileName.c_str());
if (f.good())
{
f >> json;
read(json);
return true;
}
}
catch (nlohmann::json::exception& e)
{
projectError << "Cant read json " << fileName << " Exception: " << e.what();
}
projectError << "Cant read json " << fileName;
return false;
}
on "exception" there is an explanation as below:
class "nlohmann::basic_json<std::map, std::vector, std::__cxx11::string, bool, int64_t, uint64_t, double, std::allocator, nlohmann::adl_serializer>" has no member "exception"
But I'm pretty sure that there is 'exception' in nlohmann/json.hpp as I checked.
Also there is no issue on nlohmann::json json;
part.
What am I doing wrong?
Thanks
Upvotes: 1
Views: 2252
Reputation: 31
I had the same problem. By default #include <nlohmann/json.hpp>
searches for specified file in all system paths. In my case (Ubuntu 18.04) it was /usr/include/nlohmann/json.hpp that has version 2.1.1 and generated the same error about 'nlohmann::json::exception& e'. But I had single_include version in my local directory: ~/projects/json/single_include/nlohmann/json.hpp that had version 3.9.1 without that error.
So, just change your include statement to use right version:
#include "your_src_dir/single_include/nlohmann/json.hpp"
Alternatively, specify include paths with the g++ -I
flag.
Upvotes: 1