You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
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
Duplicate symbols error happens while to_json/from_json method implemented inside entity definition header file
woodywu
opened this issue
Mar 31, 2017
· 5 comments
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...
#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) {
#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;
nlohmann, woodywu, dab0bby, gallegretti, ktonon, MohiTheFish, fwx, amirmasoudabdol, 3n16m4, arginite, and 12 more reacted with thumbs up emoji
lucidBrot reacted with confused emoji
3n16m4, arginite, cr-edeneitani, and Earl-chen reacted with rocket emoji
All reactions
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