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

"Multiple declarations" error when using types defined with typedef #611

Closed
glfmn opened this issue Jun 7, 2017 · 5 comments
Closed

"Multiple declarations" error when using types defined with typedef #611

glfmn opened this issue Jun 7, 2017 · 5 comments

Comments

@glfmn
Copy link

glfmn commented Jun 7, 2017

I have types in my code that use typedef statements to specialize some of the standard container types. I get a "Multiple declarations" error for to_json when I attempt to convert one of these to json so I can output them into a results file.

Example:

typdef vector<float> State;
typdef vector<float> Offset;

and I use it inside of other types like

struct Node {
    Offset offset;
    State state;
    float cost;
}

I tried defining a to_json instance for my Node type like so:

void to_json(json& j, const Node& n) {
    j = json{{"cost", n.cost}, {"state", n.state}, {"offset", n.offset}};
}

but I get an extremely verbose error telling me I have multiple declarations of the to_json instance. It's similar to the error I would get when using the implicit conversion of n.state = j.at("state") without using get<vector<float>>().

I also tried explicitly copying the values to no avail.

void to_json(json& j, const Node& n) {
    vector<float> state = n.state;
    vector<float> offset = n.offset;
    j = json{{"offset", offset}, {"state", n.state}};
}

The full error message is really long but I can add it upon request.

@nlohmann
Copy link
Owner

nlohmann commented Jun 7, 2017

I cannot reproduce the error. This code works (Clang):

#include "json.hpp"

using json = nlohmann::json;

typedef std::vector<float> State;
typedef std::vector<float> Offset;

struct Node {
    Offset offset;
    State state;
    float cost;
};

void to_json(json& j, const Node& n) {
    j = json{{"cost", n.cost}, {"state", n.state}, {"offset", n.offset}};
}

int main()
{
    Node n;
    n.offset = {1.1, 2.2, 3.3};
    n.state = {4.4, 5.5};
    n.cost = 47.11;
    
    json j = n;
    std::cout << std::setw(2) << j << std::endl;
}

Output:

{
  "cost": 47.1100006103516,
  "offset": [
    1.10000002384186,
    2.20000004768372,
    3.29999995231628
  ],
  "state": [
    4.40000009536743,
    5.5
  ]
}

@gregmarr
Copy link
Contributor

gregmarr commented Jun 7, 2017

Did you put the to_json implementation in a header file and not use the inline keyword?

@glfmn
Copy link
Author

glfmn commented Jun 9, 2017

@gregmarr that worked, thank you very much! Is there any reason in particular that I have to use an inline statement in header files?

Anyway, the error message has changed, I think I'm close to figuring it out now, I think it's just an issue with make not being able to find the definitions...

@gregmarr
Copy link
Contributor

gregmarr commented Jun 9, 2017

The reason for inline is that you're telling the compiler that you expect this definition to appear in multiple "translation units" and that it should eliminate all the extra definitions.
http://en.cppreference.com/w/cpp/language/inline

@glfmn
Copy link
Author

glfmn commented Jun 9, 2017

@gregmarr I've got everything working now, thank you so much!

@glfmn glfmn closed this as completed Jun 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants