no matching overloaded function found when implementing from_json function #3280
-
Hi all, I have been using nlohmann's json library for the last few days on Linux and it works great! Switched to Windows yesterday to make sure my code can cross-compile and it doesn't! Here's a minimal version to replicate the problem: #include <nlohmann/json.hpp>
namespace messages
{
using nlohmann::json;
struct JobDetails
{
std::string config;
double config_version;
std::string job_type_id;
};
} // namespace messages
namespace nlohmann
{
inline void from_json(const json& j, messages::JobDetails& x)
{
x.config = j.at("config").get<std::string>();
x.config_version = j.at("configVersion").get<double>();
x.job_type_id = j.at("jobTypeId").get<std::string>();
}
} // namespace nlohmann
int main(int argc, char** argv) { return 0; }
I use CMake to compile and VS2019 as my compiler. The library version is
Moving the And in case you ask why I have such a weird code structure, the code is actually being auto generated from a json schema using this website: https://app.quicktype.io/ Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
The from_json function must be defined in the same namespace as the type, not nlohmann. |
Beta Was this translation helpful? Give feedback.
-
"The from_json function must be defined in the same namespace as the type, not nlohmann. " => Firstly I did not understand that the code imported from https://app.quicktype.io/ needed to be corrected, because initially it worked fine. Then this compilation error no "matching overloaded function" for "get" appeared on existing code. I traced down to switching one Visual Studio 2022 project option "language conformance" "permissive" rather than default option (using language C++20). Reverting this option back to default "not permissive" recovered the compilation. Now I am questioning whether
I want to get a good understanding of the situation because I plan to rely on this lib all my life ! |
Beta Was this translation helpful? Give feedback.
-
@nlohmann Even worse, they have an option to move the functions into
|
Beta Was this translation helpful? Give feedback.
-
This is fixed: glideapps/quicktype#1996 |
Beta Was this translation helpful? Give feedback.
The from_json function must be defined in the same namespace as the type, not nlohmann.