x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Cannot Call Function After Cast C++

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.

Product Version: UE 4.17
Tags:
more ▼

asked Nov 20 '17 at 07:27 PM in C++ Programming

avatar image

TheSecondSense
150 6 10 17

avatar image Unzkilled Nov 21 '17 at 11:39 AM

as mentioned below, remove the static keyword

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

3 answers: sort voted first

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.

more ▼

answered Nov 20 '17 at 08:19 PM

avatar image

Ninja_K
80 5 8

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Nov 21 '17 at 10:07 AM

avatar image

BugSwat
210 9 14 20

avatar image TheSecondSense Nov 21 '17 at 04:27 PM

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.

avatar image Unzkilled Nov 21 '17 at 10:12 PM

Remove static keyword

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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.

more ▼

answered Nov 21 '17 at 05:34 PM

avatar image

TheSecondSense
150 6 10 17

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question