i creating simple util.h file contain aplusb(int, int) function c++ project. cannot compile , error message multiple definition of `aplusb(int, int)'. please me correct error or give me hints?
i attach here project detail reference.
file util.h
#ifndef util_h_ #define util_h_ int aplusb(int a, int b) { return + b; } #endif /* util_h_ */ file classa.h
#ifndef classa_h_ #define classa_h_ class classa { public: classa(); virtual ~classa(); private: int sum; }; #endif /* classa_h_ */ file classa.cpp
#include "classa.h" #include "util.h" classa::classa() { // todo auto-generated constructor stub sum = aplusb(3,5); } classa::~classa() { // todo auto-generated destructor stub } file classb.h
#ifndef classb_h_ #define classb_h_ class classb { public: classb(); virtual ~classb(); private: int sum; }; #endif /* classb_h_ */ file classb.cpp
#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
first variant - use inline specifier
#ifndef util_h_ #define util_h_ inline int aplusb(int a, int b) { return + b; } #endif /* util_h_ */ second variant - write definition in .cpp file.
Comments
Post a Comment