# Cannot Call Function After Cast C++

**URL:** <https://forums.unrealengine.com/t/cannot-call-function-after-cast-c/410145>\
**Category:** C++\
**Tags:** lnk2019, unreal-engine, cast, UE4, CPP, question\
**Created:** [November 20, 2017, 7:27pm UTC](https://forums.unrealengine.com/t/cannot-call-function-after-cast-c/410145 "2017-11-20T19:27:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RetroDev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/retrodev/32/1041115_2.png) [@RetroDev](https://forums.unrealengine.com/u/RetroDev)\
**Post date:** [November 20, 2017, 7:27pm UTC](https://forums.unrealengine.com/t/cannot-call-function-after-cast-c/410145/1 "2017-11-20T19:27:24Z")

</div>

Hello All!  
I am currently trying to call a function after casting to my PlayerController class from my Character. Here is my code:  
The Character cpp:

```
AInGamePlayerController * APC = Cast<AInGamePlayerController>(this);

   if (&APC) {
   GLog->Log("Casting To PlayerController Working");
   APC->HandleDeath();
   }
   else {
  GLog->Log("Casting To PlayerController Not Working");
  }

```

Everything works fine until I get to the point of adding:

```
APC->HandleDeath();

```

where I get the error:

```
error LNK2019: unresolved external symbol "public: static void __cdecl AInGamePlayerController::HandleDeath(void)" (?HandleDeath@AInGamePlayerController@@SAXXZ) referenced in function "public: void__cdecl AInGameCharacter::DoDeath(void)" (?DoDeath@AInGameCharacter@@QEAAXXZ)

```

Here is the code from my PlayerController:

```
.h
    public:
        static void HandleDeath();

.cpp
    void HandleDeath() {
    GLog->Log("PC DEATH!!");
    }

```

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![Ninja\_K](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ninja\_K](https://forums.unrealengine.com/u/Ninja_K)\
**Post date:** [November 20, 2017, 8:19pm UTC](https://forums.unrealengine.com/t/cannot-call-function-after-cast-c/410145/2 "2017-11-20T20:19:24Z")

</div>

If you are casting in your Character class, your first line is wrong. You are trying to cast your character class, not your controller class there. Just put Controller instead of this in the argument for cast in the first line. So do this:

```

AInGamePlayerController * APC = Cast<AInGamePlayerController>(Controller);

```

That should work.

---

<div class="post-metadata">

**Author:** ![BugSwat](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@BugSwat](https://forums.unrealengine.com/u/BugSwat)\
**Post date:** [November 21, 2017, 10:07am UTC](https://forums.unrealengine.com/t/cannot-call-function-after-cast-c/410145/3 "2017-11-21T10:07:50Z")

</div>

I don’t know what exactly is the problem, but I’d change two things and if the error is not gone, post here again:

```
if (&APC) {

```

I did not know this could compile. You are asking if the address of the pointer APC is true.  
That is always true.  
Make this

```
if (APC) {

```

or even better, give your pointers a “p” and use the complete question:

```
if (pAPC != nullptr) {

```

This is static:

```
  static void HandleDeath();

```

If you really want it to be static, then you don’t need a pointer and no casting, just use

```
PlayerController::HandleDeath() 

```

in your code.  
But I don’t think it should be static at all. I am not sure, I just think it should not be static.

---

<div class="post-metadata">

**Author:** ![Unzkilled](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Unzkilled](https://forums.unrealengine.com/u/Unzkilled)\
**Post date:** [November 21, 2017, 11:39am UTC](https://forums.unrealengine.com/t/cannot-call-function-after-cast-c/410145/4 "2017-11-21T11:39:08Z")

</div>

as mentioned below, remove the static keyword

---

<div class="post-metadata">

**Author:** ![RetroDev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/retrodev/32/1041115_2.png) [@RetroDev](https://forums.unrealengine.com/u/RetroDev)\
**Post date:** [November 21, 2017, 4:27pm UTC](https://forums.unrealengine.com/t/cannot-call-function-after-cast-c/410145/5 "2017-11-21T16:27:28Z")

</div>

Thanks for your answer. I am still learning and don’t know much about how to correctly use pointers and static functions. The code compiles fine, it just has an issue with:  
APC-\>HandleDeath();

Commenting out this line will fix the compile error.  
I have also tried AInGamePlayerController::HandleDeath(); with the static keyword and this also throws an error.  
I am able to call variables from the controller and print that to the log, just tried with an int32 variable. Its just the function that can’t be called for some reason.

---

<div class="post-metadata">

**Author:** ![RetroDev](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/retrodev/32/1041115_2.png) [@RetroDev](https://forums.unrealengine.com/u/RetroDev)\
**Post date:** [November 21, 2017, 5:34pm UTC](https://forums.unrealengine.com/t/cannot-call-function-after-cast-c/410145/6 "2017-11-21T17:34:16Z")

</div>

Ok. Without changing anything except what I mentioned in the comment to the other answer; it is now working. I have no idea how, it just is. I restarted VS and installed VAX. Honestly; I have no idea why its now working.

---

<div class="post-metadata">

**Author:** ![Unzkilled](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Unzkilled](https://forums.unrealengine.com/u/Unzkilled)\
**Post date:** [November 21, 2017, 10:12pm UTC](https://forums.unrealengine.com/t/cannot-call-function-after-cast-c/410145/7 "2017-11-21T22:12:05Z")

</div>

Remove static keyword
