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"

How can I avoid an error when casting from AHUD to CustomHUD?


I am trying to cast my HUD class from AHUD class to AMyProjectHUD inside my character class. But, I am getting some errors when doing it. Here is the code

 APlayerController* MyPC = Cast<APlayerController>(Controller);
         AMyProjectHUD* MyHUD;
         if (MyPC)
             MyHUD = Cast<AMyProjectHUD>(MyPC->GetHUD());
         if (MyHUD)
             const FVector2D Center = MyHUD->GetCenter();

I am getting the following errors

1>d:\ue4\new folder\myproject\source\myproject\myprojectcharacter.cpp(77): error C2220: warning treated as error - no 'object' file generated 1>d:\ue4\new folder\myproject\source\myproject\myprojectcharacter.cpp(77): warning C4701: potentially uninitialized local variable 'MyHUD' used 1>d:\ue4\new folder\myproject\source\myproject\myprojectcharacter.cpp(77): warning C4703: potentially uninitialized local pointer variable 'MyHUD' used

Product Version: Not Selected
more ▼

asked May 07 '14 at 09:17 AM in C++ Programming

avatar image

428 77 118 148

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

1 answer: sort voted first

The compiler is telling you that at the point that you check for MyHUD being non-null, there is a potential for it to be uninitialized. In C++ pointers that are uninitialized will be garbage stack memory and thus you could end up with a crash trying to dereference a pointer that doesn't point to anything.

You can fix this by simply initializing MyHUD to nullptr:

 AMyProjectHUD* MyHUD = nullptr;
more ▼

answered May 07 '14 at 10:02 AM

avatar image

BrɑκеPɑd STAFF
1.4k 33 22 78

(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