Multiple function error detection, even when using #if guard clauses

I am creating a simple UTIL.h file containing the aplusb (int, int) function for my C ++ project. However, I cannot compile, and the error message is about the multiple definition of `aplusb (int, int) '. Could you help me correct a mistake or give me some tips?

I am enclosing my project here for your detailed reference.

File util.h

#ifndef UTIL_H_ #define UTIL_H_ int aplusb(int a, int b) { return a + b; } #endif /* UTIL_H_ */ 

ClassA.h File

 #ifndef CLASSA_H_ #define CLASSA_H_ class ClassA { public: ClassA(); virtual ~ClassA(); private: int sum; }; #endif /* CLASSA_H_ */ 

ClassA.cpp File

 #include "ClassA.h" #include "UTIL.h" ClassA::ClassA() { // TODO Auto-generated constructor stub sum = aplusb(3,5); } ClassA::~ClassA() { // TODO Auto-generated destructor stub } 

ClassB.h File

 #ifndef CLASSB_H_ #define CLASSB_H_ class ClassB { public: ClassB(); virtual ~ClassB(); private: int sum; }; #endif /* CLASSB_H_ */ 

ClassB.cpp File

 #include "ClassB.h" #include "UTIL.h" ClassB::ClassB() { // TODO Auto-generated constructor stub sum = aplusb(5,6); } ClassB::~ClassB() { // TODO Auto-generated destructor stub } 

Compile error message

 ClassB.o: In function `aplusb(int, int)': /home/vtvan/Desktop/workspace/commonfunc/UTIL.h:11: multiple definition of `aplusb(int, int)' ClassA.o:/home/vtvan/Desktop/workspace/commonfunc/UTIL.h:11: first defined here collect2: error: ld returned 1 exit status make: *** [commonfunc] Error 1 
+6
source share
5 answers

First option - use the inline specifier

 #ifndef UTIL_H_ #define UTIL_H_ inline int aplusb(int a, int b) { return a + b; } #endif /* UTIL_H_ */ 

The second option is to write the definition to a .cpp file.

+17
source

You created the aplusb function in the include file. This means that for each file in which you include it, aplusb public function will be created, which will lead to a name clash.

If the function should be built-in, then mark it like this. If the function should be a template, then mark it like this. If the function should be like you wrote it, put it in a cpp file and just save the protoype in the h file.

 .h #ifndef UTIL_H_ #define UTIL_H_ int aplusb(int a, int b); #endif .cpp int aplusb(int a, int b) { return a+b; } 
+4
source

You must declare your aplusb function in the header file and provide the definition in the cpp file. Sort of

util.h:

 #ifndef UTIL_H_ #define UTIL_H_ int aplusb(int, int); #endif /* UTIL_H_ */ 

The error message tells you that every time you include the util.h file, you override the function, which is exactly what you are doing :-) This is an ODR violation (one definition rule), which says that the definition (functions in in this case) must be unique. Otherwise, the compiler will not be able to choose between alternatives (even if, as in this case, they are equal).

Please note that the templates complicate the issue a bit (in short, because the template is not a definition until it is created).

+2
source

Header files are not intended to use actual functions in them (some aspects of C ++, such as templates that do not stand up). Common practice in your case will force you to change UTIL.H to simply prototype the function ( int aplusb(int a, int b); ) and move its implementation to the source file.

0
source

You can also create a Util structure where each function is declared static. Then you can access each function using Util::<function name>

File util.h

 #ifndef UTIL_H_ #define UTIL_H_ struct Util{ static int aplusb(int a, int b) { return a + b; } }; #endif /* UTIL_H_ */ 

ClassA.cpp File

 #include "ClassA.h" #include "UTIL.h" ClassA::ClassA() { sum = Util::aplusb(3,5); } ClassA::~ClassA() { } 

ClassB.cpp File

 #include "ClassB.h" #include "UTIL.h" ClassB::ClassB() { sum = Util::aplusb(5,6); } ClassB::~ClassB() { } 
0
source

Source: https://habr.com/ru/post/944355/


All Articles