| | |
Multiple declaration error during linking
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 19
Reputation:
Solved Threads: 0
Hello,
I am stuck with a multiple declaration error during linking.
I have 4 C++ source and corresponding header files(A, B, C,D) and one main source file(Main.cpp).
Apart from that, I have one header file which contains two function declarations and some #define constants (Config.h) and 2 array decl which are used in the functions itself.
I have #included the he Config.h in Main.cpp, A.h, B.h, C.h.
I have also #included A.h in Main.cpp
Actually, I am using one function from Config.h in Main.cpp and another function in A.cpp.
Now, in this situation I have compiled all the source files separately.
When I am trying to create the exec, during linking of A.o, B.o, C.o, D.o and Main.o...I get error of multiple definition of functions and array declarations.
Please suggest to overcome this error!!!
I am stuck with a multiple declaration error during linking.
I have 4 C++ source and corresponding header files(A, B, C,D) and one main source file(Main.cpp).
Apart from that, I have one header file which contains two function declarations and some #define constants (Config.h) and 2 array decl which are used in the functions itself.
I have #included the he Config.h in Main.cpp, A.h, B.h, C.h.
I have also #included A.h in Main.cpp
Actually, I am using one function from Config.h in Main.cpp and another function in A.cpp.
Now, in this situation I have compiled all the source files separately.
When I am trying to create the exec, during linking of A.o, B.o, C.o, D.o and Main.o...I get error of multiple definition of functions and array declarations.
Please suggest to overcome this error!!!
Put only function prototypes and extern object declarations in header files, such as
Now, in one and only one *.cpp file you have to declare the same without the extern keyword.
C++ Syntax (Toggle Plain Text)
// A.h file extern void foo(); extern int f1;
Now, in one and only one *.cpp file you have to declare the same without the extern keyword.
C++ Syntax (Toggle Plain Text)
// A.cpp file #include "A.h" int f1 = 0; void foo() { }
Last edited by Ancient Dragon; May 5th, 2009 at 8:52 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
>>How to do this way???
You don't! Function implementations can not be put in header files for the reason you have already discovered -- duplicate definition errors.
You don't! Function implementations can not be put in header files for the reason you have already discovered -- duplicate definition errors.
Last edited by Ancient Dragon; May 5th, 2009 at 10:07 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: A simple grading system
- Next Thread: understanding statement in c++
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






