+ 1
How to resolve duplicate definition error raised through two different header?
NOTE: I don't want to use pre-processor directives inside the header files. Example: //Header_1.h #ifndef HEADER1 #define HEADER1 struct Saga { int h; }; #endif //Header_2.h #ifndef HEADER2 #define HEADER2 struct Saga { int h; }; #endif //main.cpp #include <Header_1.h> #include <Header_2.h>
7 Antworten
0
Here I got the solution by using macro in my main file.
#include <iostream>
using namespace std;
#define Saga Great
struct Saga {
int a;
};
#undef Saga
struct Saga {
int a;
};
int main() {
Saga obj;
cout<<&obj<<endl;
return 0;
}
+ 1
You have to change the struct name because the struct name cannot be the same, so just change one of them
+ 1
Agent_I I don't want to change the structure name.
Have you read the Note?
+ 1
𝕿𝖍𝖊 𝕲𝖗𝖊𝖆𝖙 𝕾𝖆𝖌𝖆
If you want to have two different struct, you have to change one of them, but if you want to have just one struct, you have to define it in one of the header not both.
I read the note and you said you don't want to use preprocessor in your header file. Well you can, but it could increase your compilation time, or you can just use
#pragma once
instead of using this
#ifndef HEADER
#define HEADER
...
#endif
+ 1
Agent_I you are right. I do not want to change header at all. Is there any other way to resolve it from user application?
+ 1
𝕿𝖍𝖊 𝕲𝖗𝖊𝖆𝖙 𝕾𝖆𝖌𝖆
The only problem is the redifinition of a struct, it's like creating a code like this
struct Saga {
...
};
struct Saga {
...
};
int main() {
Saga obj;
return 0;
}
The compiler cannot decide whether the obj is the first struct or the second. Unless you either change or delete one of it, there is no solution. Why would you want to do that anyway?
+ 1
Ok thanks