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

Duplicate symbols error happens while to_json/from_json method implemented inside entity definition header file #542

Closed
woodywu opened this issue Mar 31, 2017 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@woodywu
Copy link

woodywu commented Mar 31, 2017

Can't upload the project file, so I try my best to make this clear...

I implemented the to_json/from_json methods inside the entity definition header file. Suppose the file called A.h, and include this file inside the main.cpp source file. I have another source file called B.h, it does nothing but include A.h and define a class called B. When I compile the whole project, I got a duplicate symbols error.

1>ConsoleApplication10.obj : error LNK2005: "void __cdecl from_json(class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer> const &,class A &)" (?from_json@@YAXABV?$basic_json@Vmap@std@@Vvector@2@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@_N_J_KNVallocator@2@Uadl_serializer@nlohmann@@@nlohmann@@AAVA@@@Z) 已经在 B.obj 中定义 1>ConsoleApplication10.obj : error LNK2005: "void __cdecl to_json(class nlohmann::basic_json<class std::map,class std::vector,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool,__int64,unsigned __int64,double,class std::allocator,struct nlohmann::adl_serializer> &,class A const &)" (?to_json@@YAXAAV?$basic_json@Vmap@std@@Vvector@2@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@_N_J_KNVallocator@2@Uadl_serializer@nlohmann@@@nlohmann@@ABVA@@@Z) 已经在 B.obj 中定义 1>D:\Documents\Visual Studio 2015\Projects\ConsoleApplication10\Debug\ConsoleApplication10.exe : fatal error LNK1169: 找到一个或多个多重定义的符号

And I solved this by set to_json/from_json as static methods. But, according to the tutorial, I don't have to do this. I don't know if this is a document error, or I use this in a wrong way.

Sorry for my poor English...

A.h

#pragma once

#include "json.hpp"

using json = nlohmann::json;


class A
{};

void to_json(json& j, const A& val) {
}

void from_json(const json& j, A& val) {
}

B.h

#pragma once

#include "A.h"

class B
{
public:
	B();
	~B();
};

B.cpp

#include "stdafx.h"
#include "B.h"


B::B()
{
}


B::~B()
{
}

main file

#include "stdafx.h"

#include "A.h"

using json = nlohmann::json;

int main()
{
	json _ = A();

    return 0;
}
@theodelrieu
Copy link
Contributor

Hi, there's two solutions to that:

  • add inline before the function definition
  • implement from/to_json in a .cpp file

@woodywu
Copy link
Author

woodywu commented Mar 31, 2017

Thanks for your answer. BTW, can it be a available solution to solve this problem by adding [static] before the function?

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Mar 31, 2017
@theodelrieu
Copy link
Contributor

Honestly I don't know if static can cause trouble in this case, I almost never define static methods in headers.
I think you won't have multiple definitions though

@woodywu
Copy link
Author

woodywu commented Mar 31, 2017

OK, Thanks.

@woodywu woodywu closed this as completed Mar 31, 2017
@nlohmann nlohmann removed the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Apr 7, 2017
@amirmasoudabdol
Copy link
Contributor

Is it a good idea to add a short note to the documentation about this?

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Jun 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

4 participants