-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
no matching function for call to ‘nlohmann::basic_json<>::basic_json(<brace-enclosed initializer list>)’ #2559
Comments
The functions |
@nlohmann I have modified the files as below - MTop.cpp #include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace MTop_ns
{
typedef enum
{
STATE_LOCKED = 0,
STATE_UNLOCKED = 1,
STATE_SHUTTINGDOWN = 2
} MAdminStateEnum;
NLOHMANN_JSON_SERIALIZE_ENUM ( MAdminStateEnum, {
{STATE_LOCKED, "locked"},
{STATE_UNLOCKED, "unlocked"},
{STATE_SHUTTINGDOWN, "shutdown"},
});
struct MTop000
{
int mOInstId;
std::string mOInstName;
MAdminStateEnum mAdminState;
};
void to_json(json& j, const MTop000& mo)
{
j = json{{"mOInstId", mo.mOInstId},
{"mOInstName", mo.mOInstName},
};
}
void from_json(const json& j, MTop000& mo)
{
j.at("mOInstId").get_to(mo.mOInstId);
j.at("mOInstName").get_to(mo.mOInstName);
}
} MTop.hpp #include <MObjectCls.h>
class MTop : public MObjectCls
{
public:
private:
MObjectCls mOClass;
}; Now, I don't see any compilation errors as the from_json and to_json are not part of the member functions. MTop_ns::MTop000 mT = {"10, "mTopClass", STATE_LOCKED"};
json j = mT; Since I have multiple managed objects, there will be many class files. If I had to write to_json/from_json for all those, then I need to do the same for all right ?
|
Yes, you have to define it for every type. Maybe this article helps: https://json.nlohmann.me/features/arbitrary_types/#simplify-your-life-with-macros |
@nlohmann Looking into that, I made changes to my code and here is the change - Headerfile (MTop.hpp) #include <MObjectCls.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace MTop_ns
{
class MTop : public MObjectCls
{
public:
NLOHMANN_DEFINE_TYPE_INTRUSIVE(MTop, moClass)
private:
MObjectCls mOClass;
};
} Headerfile (MObjectCls.h) class MObjectCls
{
public:
MObjectCls( );
MObjectCls(int value);
virtual ~MObjectCls( );
}; WIth this, I am observing below errors -
If I modify as below, then I don't see any error. Headerfile (MTop.hpp) #include <MObjectCls.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace MTop_ns
{
class MTop : public MObject
{
public:
NLOHMANN_DEFINE_TYPE_INTRUSIVE(MTop, moClass)
private:
int mOClass;
};
} This is a nested class where we need to access parameters from base class.. Should this macro be called inside all the inherited classes with the namespace ?
|
Yes. |
@nlohmann According to the document, we see that if the macro NLOHMANN_DEFINE_TYPE_INTRUSIVE is used, there is no need to define the macro within the namespace. Currently, I am following the same. Is this correct ? NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(name, member1, member2, ...) is to be defined inside of the namespace of the class/struct to create code for. #include <MObjectCls.h>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
class MTop : public MObjectCls
{
public:
NLOHMANN_DEFINE_TYPE_INTRUSIVE(MTop, moClass)
private:
int mOClass;
};
To resolve the non-static data member error, I moved the private section of the class ahead of public. But that didn't helped. Are these errors related with not using namespace ?
|
Looks like a typo. In |
@nlohmann NLOHMANN_DEFINE_TYPE_INTRUSIVE(MTop, mOClass) Need your inputs on my below query - According to the document, we see that if the macro NLOHMANN_DEFINE_TYPE_INTRUSIVE is used, there is no need to define the macro within the namespace. Currently, I am following the same. Is this correct ?
|
What compiler are you using? Looks like there's a problem with the macro expansion. |
Can you try replacing that macro with this?
If that doesn't work, then try this:
This will at least narrow down the failure. |
Thanks for your inputs.
|
@gregmarr $ gcc --version $ uname -a |
I tried your suggestion but end up getting different error MTop.hpp #include <MObjectCls.h> using json = nlohmann::json; class MTop : public MObjectCls
private:
}; MObjectCls.h class MObjectCls MObjectCls( ); MObjectCls(int value); virtual ~MObjectCls( ); Below error is observed -
mOClass is integer. Its Unsigned integer. It should be supported by JSON API right ?
|
Right, I forgot to change |
@gregmarr This resolved the compilation error.. Hopefully, I should be able to integrate successfully. |
Hi Team,
I am trying to convert json to managed object and in this regard, I am using the to_json and from_json functions provided by nlohmann/json.
When I write a sample program, I am able to see no issues with this APIs but when I integrate with the actual project, observing the below error when i call get_to with int as datatype.
no matching function for call to ‘nlohmann::basic_json<>::basic_json()’
CPP file (MTop.cpp)
Headerfile (MTop.hpp)
Since there to_json and from_json are used by the object of this class only, these two functions are not maintained under any namespace.
if the from_json( ) logic is modified as below -
j.at("OClass").get_to(mo.mOClass);
MTop.cpp -->
Please guide me what could be the issue.
Note: Since the header and cpp files are big, I have shared only the contents which are of focus here.
The text was updated successfully, but these errors were encountered: