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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Assigning Value to TArray

In my class header I declare the variable TArray TeamCount; Then in my cpp file I initialize the TArray using TeamCount.AddZeroed(NumTeams); Then I try to increment that value TeamCount[TestPlayerState->GetTeamNum()]++; I get the following error on TeamCount.AddZeroed(NumTeams);

 Error    3    error C2662: 'int32 TArray<ElementType,FDefaultAllocator>::AddZeroed(int32)' : cannot convert 'this' pointer from 'const TArray<ElementType,FDefaultAllocator>' to 'TArray<ElementType,FDefaultAllocator> &'    

and then I get the following error on TeamCount[TestPlayerState->GetTeamNum()]++;

 Error    4    error C3892: 'TArray<ElementType,FDefaultAllocator>::operator []' : you cannot assign to a variable that is const

I don't know why this is happening, I am thinking i declared the variable wrong.

Product Version: UE 4.9
more ▼

asked Oct 18 '15 at 09:56 PM in C++ Programming

avatar image

492 66 64 211

avatar image Shadowriver Oct 19 '15 at 01:43 AM

You sure you didn't do const array?

avatar image Janyx Oct 19 '15 at 05:05 AM

Ya I triple checked, that is why I am so confused. It's acting like it is const. I didn't know if there were special rules for working with TArray

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

1 answer: sort voted first

Might need to see some context on both TeamCount's declaration, and the function in which it's being accessed. My first guess is that if the this pointer is const at that point, all member vars will be as well. Maybe check the function signature?

 void MyClass::DoSomething()
     TeamCount.AddZeroed(1); // Will work fine
 void MyClass::DoSomething() const
     TeamCount.AddZeroed(1); // Compile error
more ▼

answered Oct 19 '15 at 06:07 AM

avatar image

41 2 3 4

avatar image Janyx Oct 19 '15 at 04:51 PM

Yup that was the issue. Thanks for the help!

(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