Unresolved External Symbol When Trying to Create an Abstract Actor Class With a Pure Virtual Function

So, I’m trying to create an interface class called ‘AInteractableActor’ which contains a pure virtual method named ‘Interact()’ so that I can easily add interactable actors to my game without having to program an individual cast for each type of actor I want to be able to interact with inside my PlayerCharacter class.

However, when I compile my code I’m given a linking error to do with an unresolved external symbol. This is the error output from VS:

Error	1	error LNK2019: unresolved external symbol "public: __cdecl AInteractableActor::AInteractableActor(void)" (??0AInteractableActor@@QEAA@XZ) referenced in function "public: static void __cdecl AInteractableActor::__DefaultConstructor(class FObjectInitializer const &)" (?__DefaultConstructor@AInteractableActor@@SAXAEBVFObjectInitializer@@@Z)	D:\Unreal Projects\Testing\Intermediate\ProjectFiles\PlayerCharacter.cpp.obj	Testing

Error	2	error LNK2001: unresolved external symbol "public: __cdecl AInteractableActor::AInteractableActor(void)" (??0AInteractableActor@@QEAA@XZ)	D:\Unreal Projects\Testing\Intermediate\ProjectFiles\Testing.generated.cpp.obj	Testing

Error	3	error LNK1120: 1 unresolved externals	D:\Unreal Projects\Testing\Binaries\Win64\UE4Editor-Testing-8366.dll	1	1	Testing

Error	4	error : Failed to produce item: D:\Unreal Projects\Testing\Binaries\Win64\UE4Editor-Testing-8366.dll	D:\Unreal Projects\Testing\Intermediate\ProjectFiles\ERROR	Testing

Error	5	error MSB3073: The command ""C:\Program Files\Epic Games\4.8\Engine\Build\BatchFiles\Build.bat" TestingEditor Win64 Development "D:\Unreal Projects\Testing\Testing.uproject" -rocket" exited with code -1.	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.MakeFile.Targets	38	5	Testing

As far as I can tell, it’s having a problem finding the default constructor for the class for some reason.

This is the code for my InteractableActor.h:

#pragma once

#include "GameFramework/Actor.h"
#include "InteractableActor.generated.h"

UCLASS(ABSTRACT)
class TESTING_API AInteractableActor : public AActor
{
	GENERATED_BODY()

public:	
	AInteractableActor();
	UFUNCTION(BlueprintCallable, Category = Interaction)
		virtual void Interact() PURE_VIRTUAL(AInteractableActor::Interact, ;);
};

And this is the class for InteractableActor.cpp:

#include "Testing.h"
#include "InteractableActor.h"

AInteractableActor::AInteractableActor()
{
	
}

This is the related piece of code I am running within my PlayerCharacter.cpp:

void APlayerCharacter::Use()
{
	FHitResult HitResult(EForceInit::ForceInit);
	
	if (TraceFromSelf(HitResult, 250.0f, ECollisionChannel::ECC_EngineTraceChannel1))
	{
		AActor* const HitActor = HitResult.GetActor();
		if (HitActor)
		{
			AInteractableActor* InteractableActor = dynamic_cast<AInteractableActor*>(HitActor);
			if (InteractableActor)
			{
				InteractableActor->Interact();
			}
		}
	}
}

Any clues on how to fix this?

Actors & Objects can’t have pure virtual functions in them. The reason is that the UClass for those objects needs to create the CDO (class default object). This happens even for Abstract classes. It’s pretty key to how the UObject property information is copied from parent classes to child classes. The best you can do to mimic pure virtual behavior is to assert in the abstract class implementation.

Could you explain to me what you mean by asserting in the abstract class implementation, please? I don’t understand why, however I got the code to function as I wanted it to by changing some things in the InteractableActor.h file and cleaning my project. The code is here: https://github.com/xxyxxyz/UnrealEngine-4-InteractableActor-Class/tree/master/Source

Here’s the code I mean

virtual void Interact() { check(0 && "Subclasses must implement this and not call Super to the base class"); }

Thanks. Does this mean that the code I have that’s currently functioning is only functioning because of a bug?

I thought you said it wasn’t compiling? If it is, then ignore this. This will allow you to treat the class as abstract base class without the virtual foo() = 0;

Sorry if I wasn’t clear. It wouldn’t compile with the code I provided in the main post however the code in the Github link in one of my previous comments does compile and function appropriately.