In your case it does not mean your array is null, 1st because it is not a pointer. The error message just says that the symbol is undefined at the moment the linker tries to find it. Such error usually happens when you try to access a class from an outside class function and the class is not exported. You need to use the module export define name in front of your class name, so anywhere in your code you might have done like below:
UCLASS()
class AMyBLE : public AActor
{
…
}
and you miss:
UCLASS()
class XXXXXXX_API AMyBLE : public AActor
{
…
}
that XXXXXXX_API you will find at the file where you have IMPLEMENT_MODULE(X, Y) or IMPLEMENT_PRIMARY_GAME_MODULE(X, Y, Z), which you should use the Y word there and add the _API suffix.
UCLASS(BlueprintType) class ANDROIDAPITEMPLATE_API USqueezy : public UObject
{
GENERATED_BODY()
public:
USqueezy();
FVector Acceleration;
FVector Displacement;
FQuat Element;
int Pressure;
int Step;
int State;
int Key;
long Time;
bool ButtonStatus;
FString MAC;
FString Name;
};
.cpp:
#include “Squeezy.h”
USqueezy::USqueezy() { }
Then I have an Actor that wants to use that object:
This works PERFECT…no errors.
So I can create an object inside my function of type USqueezie* , add parameters on it and print to debugscreen the data. No errors.
But I just created and used the object inside the same function.
My problem is when i defina a TARRAY of this object as:
static TArray<USqueezy*> MySqueezies;
in the .h
and try to add in the function the generated squeezie inside the array like this:
if (MySqueezies.Find(ThisSqueezy) != INDEX_NONE) MySqueezies.RemoveAt(MySqueezies.Find(ThisSqueezy));
MySqueezies.Add(ThisSqueezy);
I see now. In your case, you made the Array a static member, and every static member for a class must be initialized or else it would be as there is no instance for it. To fix that, you need to include at the .cpp file for the class you placed that static Array the following lines to create the instance for that static member inside the class:
TArray<USqueezy *> AMyBLE_CPP::MySqueezies;
and that’s it. It is just a C++ rule, nothing specific for UE4.
You must always set TArrays to how many you need. Then if you want you can put in a nullptr in each to start them. Then once they go into actual use you can fill them with what you want.
//put this in the constructor
MySqueezies.SetNum(Howmany) ;//will initialize it to hold that many.
//will hold addresses to the actual info, put this in beginplay();
for( int32 i = 0; i <MySqueezies.Num(); i++ )
{
MySqueezies[i] = nullptr;//set them all to null ptr, ready for use now
}
If i add the line in the .cpp BeginPlay() function liek this:
TArray<USqueezy *> AMyBLE_CPP::MySqueezies;
I get this error:
/MyBLE_CPP.cpp(25,32): error: definition or redeclaration of ‘MySqueezies’ not allowed inside a function
LogPlayLevel: TArray<USqueezy*> AMyBLE_CPP::MySqueezies;
LogPlayLevel: ~~~~~~~~~~~~^
Hi! I just want to initialize without members (because I want to add/remove members later) anyway I try just MySqueezies.SetNum(1) ; to see what happens and this is the error* (*I am adding this line in the beginplay() function):
I dont understan why I MUST set how many members i need in the array when working on blueprints you just add/remove/etc on runtime without problems. Define how many members will you use in the array before use it sounds strange
when i define one as static . if i do not set how many it will be, i have compile errors. If you are trying to use the array with out setting it to something and not static, Then use add() remove() append(). add them that way.
I just noticed the main problem is not because of the Array. It is because I define the variable outside the function (as public static TArray<USqueezy*> MySqueezies; in the .h)
if i declare a regular variable like:
If you have not put anything into it or define its length. You are going to get an error when trying to use it. The array must contain something or it will error when trying to access it as there is nothing to access as you have not put anything into it.
So if you go and say set up an tarray like so
TArray<YourClass*> Testarray;
The in constructor do this or do not do this and use add() or insert or append()
Testarray.SetNum(10);
At that point you should have 10 Testarray to put stuff into. if you do not use SetNum().
Then use add(); or addunique(); or append() or insert(). and those should work.
It is necessary to declare it outside of any function, just at the .cpp body. See the article. I was not aware of the need for the TArray to set the amount of instances it would have. It might be necessary to change the way you want that to work. I will give this a thought and will let you know.
use this then addunique(); that is the only thing that will keep every instance different if they are the same it wont add it. Example if your putting in numbers it wont let you put in the same number twice.
Anyway the base problem stills…and is not related to arrays. still figuring aout where the problem is.
I have a variable (A) of class XX declared in the. .h file (public static otherwise i get errors)
then inside a function in the .cpp i have a valid (B) of class XX and I just try to assign in the funciont: A = B; and i get this : A → undefined symbol
You could use TMap instead of TArray. TMap uses a key to access the element and it won’t allow duplicates, so you won’t be able to insert a duplicate inside the container.
Static types must be initialized in the CPP file, outside any function body and outside any class body.
Also, there’s a dozen ways to initialize types in C++. After a decade of debate, the C++ Gods (creators of the language) came to the conclusion that you should initialize containers and types with a ’ {} '.
So, in the CPP file, NO function or class body, you do this:
@BrUnO_XaVIeR :…let me tell you something…YOU are my God today!
I would NEVER solve this without your help. That hint about initialize outside anything in the CPP solved the problem Thanks again man!
@NilsonLima : That link really helped me fully understand what static means