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"

C++ Static Variable Issues

Hi there! I'm currently having some issues when attempting to make a static variable. I have experience in Java and so my understanding of a static variable is something that affects every object of a class and is the same for each object. I am trying to do this for a boolean as shown in the code:

static bool color;

However I'm not entirely sure how to initialize the boolean. Whenever I try I just get a compiler error screaming at me for an unidentified variable. I would love if one of you guys could give me a hand in figuring this out.

Product Version: UE 4.13
more ▼

asked Nov 01 '16 at 07:47 PM in C++ Programming

avatar image

22 4 7 10

avatar image Sean L ♦♦ STAFF Nov 01 '16 at 08:01 PM

Hey DinosaurDan,

Where are you attempting to declare your static boolean?

Also, what exactly are you trying to use the bool for?

avatar image funkinessfactor Nov 01 '16 at 08:08 PM

I assume you are in a class.

initialise in cpp file by..

bool classname::color = true

avatar image DinosaurDan Nov 02 '16 at 02:35 PM

I am using the bool as a way to switch between if statements within the "Puzzle" template. I am also trying to declare it here: alt text

and am trying to initialize it here: alt text

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

1 answer: sort voted first

Hey DinosaurDan,

One thing you could try is to declare the variable inside of the function that you're looking to use it in. When I did that it seemed to work fine for me.

What I did was create the variable at the top of the .cpp file and then I was able to use it throughout the functions that I wanted to use it in without any compiler errors.

Here's some good reading regarding the static keyword also, if you're interested: http://thesmithfam.org/blog/2010/06/12/the-many-meanings-of-the-c-static-keyword/

Have a great day

more ▼

answered Nov 03 '16 at 01:44 PM

avatar image

Sean L ♦♦ STAFF
43.8k 485 152 440

(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