Why does this not indicate operator overloading as built-in, causing a duplicate definition error?

After creating the next class, compilation will fail with many duplicate character errors. The actual error is not very descriptive:

"__Zeq duplicate character .... in: / Users / myusername / Library / Developer / Xcode / DerivedData / MyProject -asdfasfasdf .... / Build / Intermediates / MyProject.build / Debug-iphonesimulator / MyTarget.build / Objects-normal /i386/MyClass.o "

The above message appears for many different classes and appears at the end of compilation, so I don't know what the problem is.

I checked the following:

  • Classes that use this include the Listening.hpp file.
  • The only definition for this class is in this file.

What is the problem?

#ifndef Listening_hpp #define Listening_hpp #include <stdio.h> #include "EAction.hpp" class Listening{ private: int _emitterId; int _listenerId; std::string _eventName; EAction* _actionToTake; //not owned. protected: public: Listening(int emitterId,int listenerId,std::string eventName,EAction* actionToTake) : _emitterId(emitterId),_listenerId(listenerId),_eventName(eventName){ _actionToTake = actionToTake; } int getEmitterId()const{ return _emitterId; } int getListenerId()const{ return _listenerId; } std::string getEventName()const{ return _eventName; } EAction* getAction()const{ return _actionToTake; } }; bool operator==(const Listening &first,const Listening &other) { bool result = false; if(first.getEmitterId() == other.getEmitterId()){ if(first.getListenerId() == other.getListenerId()){ if(first.getEventName() == other.getEventName()){ if (first.getAction() == other.getAction()) { result = true; } } } } return result; } bool operator!=(const Listening &first,const Listening &other) { bool result = !(first == other); return result; } #endif /* Listening_hpp */ 

EAction.hpp

 #ifndef EAction_hpp #define EAction_hpp #include <stdio.h> class EAction{ private: protected: public: virtual std::vector<std::size_t> seedList() = 0; }; #endif /* EAction_hpp */ 

EDIT: Edited title - I think this may help people who have duplicate definition errors for other reasons ignore this answer.

+5
source share
1 answer

Free functions in the header file should be marked as inline or modified to have only declarations in the header:

 inline bool operator==(const Listening &first,const Listening &other) { 

and similarly for operator!= .

The original problem is that any unit, including this header file, will have its own object file containing a copy of operator== . Then the linker sees this and does not know which one should be correct. inline can be seen as a linker directive to say: "All of these functions are the same, just select one." Link to more detailed answers.

The same thing did not happen with member function bodies, because such bodies, written inside the class definition, are implicitly inline .


Historical background: Initially, inline was primarily an optimization directive. However, today's compilers are smart enough to make their own optimization decisions; so the main use of inline now is what used to be a secondary effect: avoid multiple definition errors when there is a function in the header.


By the way, you can write return bla; instead of assigning bla to bool variable, etc.

+6
source

All Articles