Download

I have added a virtual function to AActor, why can't i compile?

Hello! I am relatively new to C++ and UE4, and for an experiment, i have tried adding this line of code in the public section of the AActor class in Actor.h


virtual void Hit();

However i get the following error when i try to compile (well, when i try and add code to the project, oddly enough, i can compile fine just with the compile button)



CompilerResultsLog:Error: Error TPExampleGameMode.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::Hit(void)" (?Hit@AActor@@UEAAXXZ)
CompilerResultsLog:Error: Error TPExample.generated.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::Hit(void)" (?Hit@AActor@@UEAAXXZ)
CompilerResultsLog:Error: Error DestroyableCube.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::Hit(void)" (?Hit@AActor@@UEAAXXZ)
CompilerResultsLog:Error: Error DestroyableObj.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::Hit(void)" (?Hit@AActor@@UEAAXXZ)
CompilerResultsLog:Error: Error DestroyableObject.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::Hit(void)" (?Hit@AActor@@UEAAXXZ)
CompilerResultsLog:Error: Error TPExampleCharacter.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::Hit(void)" (?Hit@AActor@@UEAAXXZ)
CompilerResultsLog:Error: Error C:\Users\Edwin\Documents\Unreal Projects\TPExample\Binaries\Win64\UE4Editor-TPExample-5429.dll : fatal error LNK1120: 1 unresolved externals

What’s happening?

did you write the function prototype in Actor.cpp?

I don’t know, could you give an example. DOn’t quite know what you mean.

I wrote it in Actor.h if that’s what you mean

This is what a function prototype looks like
you would write it in Actor.cpp



void AActor::Hit()

{
   //Your Function Implementation Goes Here
}


I didn’t test what you wrote but it is possible you are trying to use a name (Hit) that is already in use
try MyHit instead

Okay, i’ll try the other name

I’ve tried with a different name and tried to rebuild, but here’s my compiler output:



2>Destrtoyable.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::PunchActor(void)" (?PunchActor@AActor@@UEAAXXZ)
2>TPExampleCharacter.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::PunchActor(void)" (?PunchActor@AActor@@UEAAXXZ)
2>TPExampleGameMode.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::PunchActor(void)" (?PunchActor@AActor@@UEAAXXZ)
2>TPExample.generated.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::PunchActor(void)" (?PunchActor@AActor@@UEAAXXZ)
2>DestroyableCube.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::PunchActor(void)" (?PunchActor@AActor@@UEAAXXZ)
2>DestroyableObj.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::PunchActor(void)" (?PunchActor@AActor@@UEAAXXZ)
2>DestroyableObject.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::PunchActor(void)" (?PunchActor@AActor@@UEAAXXZ)
2>DestroyableObject2.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl AActor::PunchActor(void)" (?PunchActor@AActor@@UEAAXXZ)
2>C:\Users\Edwin\Documents\Unreal Projects\TPExample\Binaries\Win64\UE4Editor-TPExample-5590.dll : fatal error LNK1120: 1 unresolved externals


I am not sure what the problem is.
I just noticed you wrote *" i have tried adding this line of code in the public section of the AActor class in Actor.h" *
Are you editing the Actor class itself?
You should not edit the actor class.

You should create a child class and work on that

create a MyActor class that derives from Actor.

You can do that in the editor / content browser / new C++ class / Parent Class Actor
that should open up both your New Class Header and CPP file in Visual Studio

Compile them with no changes.

Then add the function declaration like you did to the .h file and the prototype to .cpp it should work.

I am editing the actor class, the idea was to use a virtual function so that the children of the actor class can have their own prototypes of Hit(), so i’m guessing this isn’t the way to do it. Thanks for the help!

It is not necessary to edit that class. And I would advise strongly against doing it.

You can accomplish what you want by making a new base class that inherits from actor
and then making classes that are children of your new base class

This is definitely the way to go.

But to refer to your errors, you’ve got linker errors because you didn’t provide an implementation. You can write in the header file e.g. virtual void Hit() {} to provide a empty one. But like Colorado said, just don’t edit the Actor class.

Create a new base class



Actor
   |
   v Unit (your class with the hit function
   |
   v Your classes which should inherit the hit function


Basically, yeah.

If you modify AActor, then you modify everything that inherits from it, including Epic’s classes. Sometimes you want to do that. That said, unless you intend to send a pull request to Epic, you have just forked the engine and take full responsibility for merging your edits with theirs for each and every update that Epic releases. Not fun. This could be like, one or more full-time jobs worth of person-hours, depending on how much you change.

If your change should only affect YOUR code, then you’ll want to create a subclass of AActor with your edits, and then subclass THAT subclass for any class that should inherit from it (as the quoted statement shows).

I believe you can just implement an empty function in the cpp file. Keep what you have in your header and add TheClass::YourFunction(){} etc… Unresolved external symbol most likely means you are using that function somewhere but there is no implementation for the linker. As long as you override it in the child class it will call the child’s version. Also, you can call the parent function if you decide to not have it empty and have some boilerplate code. Hope this helps :slight_smile:

Virtual functions have to be implemented in the cpp file. Otherwise you’ll get a linker error.

Also, there’s no point changing AActor unless you’re using source engine - because it won’t compile anyway.

i had the same problem and your solution fixed it thanks.