Error LNK2005: "public: bool __cdecl *** already defined in ***

I declared a function as

class CustomActor* Foo();

in other class.

However, the error keeps showing up as above.

Please let me know how to fix it.

thanks in advance.

I’d need to see more of your code to be sure, but either you probably multiple declarations of Foo (or whatever variable / method it’s compaining about).

Check your .h file to make sure you only use the variable / method names once.

#include “GameFramework/Pawn.h”
#include “Pawn.generated.h”

UCLASS()
class PROJ_API Pawn : public APawn{
...
class CustomActor* Foo();
...
}

--------------------------------

#include "Engine/StaticMeshActor.h"
#include "CustomActor.generated.h"

class PROJ_API CustomActor : public AstaticMeshActor{
...
AUsableActor(const class FObjectInitializer& PCIP);
...
}

Here it is

I’m assuming these are two .h files split into one. If not, you should certainly do that.

Other issues:

  1. You should start all actor classes with ‘A’ (e.g. ‘AMyPawn’, ‘ACustomActor’). This isn’t just a naming convention, Unreal cares.
  2. Don’t use names like ‘Pawn’ for your classes because once you use the correct naming convention, it becomes APawn which is a duplicate
  3. You’ve got a constructor for AUsableActor in the CustomActor class. You should change CustomActor to ACustomActor and then change the constructor to be ACustomActor()
  4. CustomActor is inheriting from AstaticMeshActor. Should be AStaticMeshActor (lower and upper case do matter)
  5. Your CustomActor should be decorated with a UCLASS() (like your Pawn class).

Fix up these issues and if it still doesn’t work, post the headers and error messages again.

Thank you so much!!
You saved my life

Glad it helped. Can you accept the answer to help others find it too.