Skip to content
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

Compilation error using at and find with enum struct #1316

Closed
capulido opened this issue Oct 23, 2018 · 1 comment
Closed

Compilation error using at and find with enum struct #1316

capulido opened this issue Oct 23, 2018 · 1 comment

Comments

@capulido
Copy link

capulido commented Oct 23, 2018

Hello

Currently I'm trying to get or find a enum struct but I get a compilation error.

error: no matching function for call to 'nlohmann::basic_json<>::at(const ns::m_struct::m_enum_struct&) const
error: no matching function for call to 'std::map<std::__cxx11::basic_string<char>, nlohmann::basic_json<>, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, nlohmann::basic_json<> > > >::find(const ns::m_struct::m_enum_struct&)

This is the function where I get the error

void myClass::getValue(const enum m_struct::m_enum_struct v){
    static const nlohmann::json j = {
        {ns::m_struct::m_enum_struct::a,500},
        {ns::m_struct::m_enum_struct::b,2000}
    };
    int p;
    if(j.find(v) != j.end()){ //error
        p = j.at(v); //error
    }else{
        p = 0;
    }
}

this is how I implement my objects, to_json and from_json:

namespace ns{
    struct m_struct{
        enum struct m_enum_struct {
            a = 2,
            b = 5,
            c = 7
        };
    };
    void from_json(const nlohmann::json & j, enum m_struct::m_enum_struct & v){
        v = static_cast<enum m_struct::m_enum_struct>(std::stoi(j.get<std::string>()));
    }
    void to_json(nlohmann::json& j, const enum m_struct::m_enum_struct & v){
        j = nlohmann::json{std::to_string(static_cast<int>(v))};
    }
}

I'm using gcc 6.4.0 and working from v 3.3.0 of the develop branch

EDIT: fixed typos

@capulido
Copy link
Author

Sorry, I forgot to convert my enum to string before doing a search inside the object:

j.find(std::to_string(static_cast<int>(v)))

That fixes the compilation error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant