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 access private member on adding to TArray

Hi, The past few days i've been trying to get used to c++ in ue4 and I cant seem to find the reason for the following problem: Im trying to add an element to a TArray, but I keep getting a compile error:

 C2248    'UDFinedItem::UDFinedItem': cannot access private member declared in class 'UDFinedItem'    Gravity    D:\Program Files\Epic Games\UE_4.18\Engine\Source\Runtime\Core\Public\Containers\Array.h    1736 

 `C2665    'UDFinedItem::operator new': none of the 2 overloads could convert all the argument types    Gravity    D:\Program Files\Epic Games\UE_4.18\Engine\Source\Runtime\Core\Public\Containers\Array.h    1736    
 

Im new to c++ so sorry if i am missing something stupid. And thanks in advance for any help The code involved is:

DFinedItem.h:

 #pragma once
 
 #include "CoreMinimal.h"
 #include "UObject/NoExportTypes.h"
 #include "DFinedItem.generated.h"
 
 /**
  * 
  */
 UCLASS()
 class GRAVITY_API UDFinedItem : public UObject
 {
     GENERATED_BODY()
         static TArray<UDFinedItem> Items;
     protected:
         void registerItem(UDFinedItem & itm);
         FString name;
         FString displayName;
         int id;
         bool stackable;
     public:
         UDFinedItem();
         ~UDFinedItem();
         virtual int getId();
         virtual FString getName();
         virtual FString getDisplayName();
 };

DFinedItem.cpp:

     #include "DFinedItem.h"
     
     static TArray<UDFinedItem> Items;// Init(10, 5);
     UDFinedItem::UDFinedItem()
     {
         registerItem(*this);
     }
     
     UDFinedItem::~UDFinedItem()
     {
     }
     
     int UDFinedItem::getId() {
         return id;
     }
     
     FString UDFinedItem::getName() {
         return name;
     }
     
     FString UDFinedItem::getDisplayName() {
         return displayName;
     }
     
     void UDFinedItem::registerItem(UDFinedItem & itm) {
         Items.Add(itm);
     }
     
     
     
     
 
 
 
 
 
 
 
 
 

Product Version: UE 4.18
Tags:
more ▼

asked Feb 23 '18 at 05:58 PM in C++ Programming

avatar image

DFined
20 3 5 8

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

2 answers: sort voted first

Not sure exactly what the problem is but one thing is certain: you can't declare an array of objects like that. They need to be object pointers.

more ▼

answered Feb 23 '18 at 09:04 PM

avatar image

Jin_VE
4.4k 32 79 42

avatar image DFined Feb 23 '18 at 10:00 PM

So would this be closer?

 // Fill out your copyright notice in the Description page of Project Settings.
 #include "DFinedItem.h"
 
 static TArray<UDFinedItem*> Items;
 UDFinedItem::UDFinedItem()
 {
     registerItem(this);
 }
 
 UDFinedItem::~UDFinedItem()
 {
 }
 
 int UDFinedItem::getId() {
     return id;
 }
 
 FString UDFinedItem::getName() {
     return name;
 }
 
 FString UDFinedItem::getDisplayName() {
     return displayName;
 }
 
 void UDFinedItem::registerItem(UDFinedItem* itm) {
     UDFinedItem::Items.Add(itm);
 }

But that gives an even stranger error:

 Error    LNK2001    unresolved external symbol "private: static class TArray<class UDFinedItem *,class FDefaultAllocator> UDFinedItem::Items" (?Items@UDFinedItem@@0V?$TArray@PEAVUDFinedItem@@VFDefaultAllocator@@@@A)    Gravity    D:\Unreal Projects\Gravity\Intermediate\ProjectFiles\DFinedItem.cpp.obj    1    
 
 

avatar image Jin_VE Feb 23 '18 at 10:40 PM

No that's better. You declared the Items array in the .h file but you didn't define it in the .cpp file correctly.

It should be TArray<UDFinedItem*> UDFinedItem::Items;

avatar image Jin_VE Feb 23 '18 at 10:42 PM

My first answer wasn't showing so I posted again, this time in more detail. Now that the original answer is back, I deleted the second one and I am copying the text from that in to this comment so the information isn't lost. Here's that answer:

There could be more than one issue but the first one I see is this: Items needs to be an array of pointers.

 static TArray<UDFinedItem*> Items;

Also, the register function should take in a pointer to the object.

This is because UE4 expects UCLASS instances to be dynamically allocated. You can work with pointers and references. In this case, I think you want to use pointers.

Pointers are the biggest difference between C++ and C#/Java so it makes sense you'd bump into this. Keep an eye out for next time.

avatar image DFined Feb 23 '18 at 10:49 PM

Oh, ok, yeah, thanks A LOT! It works now! I am very much grateful, it's nice to get actual help on a forum, especially so quickly, so again, thanks a lot!

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

The problem is that Items is not only private but also static. This means that all instances of UDFinedItem share the same Items array. As a result of this you need to access it in a different way in registerItem(). Your call

 Items.Add(itm);

is the same as

 this->Items.Add(itm);

What is not working as Items does not belong to this instance but to the class. So what you need is:

 UDFinedItem::Items.Add(itm);

What I can not judge about from the code on the first view is if it makes sense to have a static array of items. If you need this, you probably want to save (shared/weak) pointers to UDFinedItems in it, not deep copies of the objects.

more ▼

answered Feb 23 '18 at 06:49 PM

avatar image

j.mueller RFG
304 3 10 10

avatar image DFined Feb 23 '18 at 07:58 PM

Thanks for the fast answer... However, changing to UDFinedItem::Items.Add(itm); still gives the same compile error... As for doing this at all - not quite sure, as im trying to readapt from java, where this would definitely be the correct way, so i dont really know whats the bet way here

avatar image Balgy Feb 23 '18 at 08:57 PM

You only need the static keyword if you need the Array and its elements to persist across multiple class instances

avatar image DFined Feb 23 '18 at 09:37 PM

Yes, thats what i intended

(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