I have a C++ script (called
main_script.cpp) that calls two classes (called
class2.cpp/h) which both include <npp.h>.
While compiling my program, I encounter the following error:
class2.o: In function `npp::operator<<(std::ostream&, npp::Exception const&)': tmpxft_000002fb_00000000-5_class2.compute_61.cudafe1.cpp:(.text+0x10): multiple definition of `npp::operator<<(std::ostream&, npp::Exception const&)' class1.o:tmpxft_000002d2_00000000-5_class1.compute_61.cudafe1.cpp:(.text+0x10): first defined here collect2: error: ld returned 1 exit status Makefile:107: recipe for target 'main_script' failed
Googling this error lead me to the concept of #include guards. I am using include guards in all my header files, which leads me to the belief that in the NPP code, the overloading of the operator<< is not coded properly.
Here are some suggestions I found online to fix the problem:
- declaring the operator in the header file and moving the implementation to the .cpp file
- define the operator implementation inside the class definition
- inline the operator implementation
Perhaps my assumptions are all wrong and the problem is in my code, but in either case, I’d like to get your opinion! :)
By the way, if I change my code to only include 1 class (not 2), then this error doesn’t appear and my code runs smoothly.