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

json.hpp:8955: multiple definition of function that is not defined twice or more. #804

Closed
nm17 opened this issue Oct 27, 2017 · 9 comments
Closed

Comments

@nm17
Copy link

nm17 commented Oct 27, 2017

Json version 2.1.1

Any code in this form:

// This file called "lib.hpp"
using json = nlohmann::json;

namespace someNS {
    someType someFunction(json file) {
        // ...
    }
}

...Returns this error:

lib.hpp.o: In function `std::iterator_traits<char const*>::iterator_category std::__iterator_category<char const*>(char const* const&)':
json.hpp:8955: multiple definition of `someNS::someFunction(nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer>)'
main.cpp.o:lib.hpp:7: first defined here
@nlohmann
Copy link
Owner

I cannot reproduce this with:

// lib.hpp

#include "json.hpp"

using json = nlohmann::json;

namespace someNS {
    std::string someFunction(json file) {
        return file.dump(4);
    }
}
// main.cpp

#include "lib.hpp"
#include <iostream>

int main()
{
    json j = {1,2,3};
    std::cout << someNS::someFunction(j) << std::endl;
}

and version 2.1.1.

Running

c++ -std=c++11    main.cpp   -o main
./main

yields

[
    1,
    2,
    3
]

@theodelrieu
Copy link
Contributor

This looks a lot like a missing include guard, do you include json.hpp directly or through another of your own header files?

@nm17
Copy link
Author

nm17 commented Oct 27, 2017

@theodelrieu I have included it.

@theodelrieu
Copy link
Contributor

Could you paste the contents of lib.hpp?

@nm17
Copy link
Author

nm17 commented Oct 27, 2017

@theodelrieu

#include <SFML/Window.hpp>
#include "json.hpp"

using json = nlohmann::json;

namespace FB {
    std::vector<sf::Vector2f> praseTeams(json file) {
        std::vector<sf::Vector2f> ret;
        unsigned int retPos = 0;
        json tmp;
        std::string itV;
        for (json::iterator it = file.begin(); it != file.end(); ++it) {
            itV = it.value();
            tmp.parse(itV);
            ret[retPos].x = tmp["x"];
            ret[retPos].y = tmp["y"];
            retPos++;
            tmp = nullptr;
        }
        return ret;
    }
}

@theodelrieu
Copy link
Contributor

I guess this file is included multiple times, try to add a #pragma once at the very top of your file, this should work.

@nm17
Copy link
Author

nm17 commented Oct 27, 2017

@theodelrieu Here is the code. The first one was older.
Also it does not work...

#pragma once
#include <SFML/Window.hpp>
#include "json.hpp"

using json = nlohmann::json;

namespace FB {
    std::vector<sf::Vector2f> praseTeams(json file) {
        std::vector<sf::Vector2f> ret;
        unsigned int retPos = 0;
        json tmp;
        std::string itV;
        for (json::iterator it = file.begin(); it != file.end(); ++it) {
            itV = it.value();
            tmp.parse(itV);
            ret[retPos].x = tmp["x"];
            ret[retPos].y = tmp["y"];
            retPos++;
            tmp = 0;
        }
        return ret;
    }
}

@theodelrieu
Copy link
Contributor

Oh I didn't realize, it's very simple in fact.

This has nothing to do with this library, you should not define a method in a header, unless:

  • It is marked inline (in your case this doesn't seem very judicious)
  • It is a function template
  • It is a member function

@nm17
Copy link
Author

nm17 commented Oct 27, 2017

Thank you!

@nm17 nm17 closed this as completed Oct 27, 2017
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

3 participants