Duplicate symbols error happens while to_json/from_json method implemented inside entity definition header file #542
Labels
kind: question
solution: proposed fix
a fix for the issue has been proposed and waits for confirmation
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
B.h
B.cpp
main file
The text was updated successfully, but these errors were encountered: