Skip to content

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

Closed

Description

@nm17

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

Activity

  1. nlohmann commented on Oct 27, 2017

    @nlohmann
    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
    ]
  2. theodelrieu commented on Oct 27, 2017

    @theodelrieu
    Contributor

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

  3. nm17 commented on Oct 27, 2017

    @nm17
    Author

    @theodelrieu I have included it.

  4. theodelrieu commented on Oct 27, 2017

    @theodelrieu
    Contributor

    Could you paste the contents of lib.hpp?

  5. nm17 commented on Oct 27, 2017

    @nm17
    Author

    @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;
        }
    }
  6. theodelrieu commented on Oct 27, 2017

    @theodelrieu
    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.

  7. nm17 commented on Oct 27, 2017

    @nm17
    Author

    @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;
        }
    }
  8. theodelrieu commented on Oct 27, 2017

    @theodelrieu
    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
  9. nm17 commented on Oct 27, 2017

    @nm17
    Author

    Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions