Download

Syntax error when calling UStruct functions.

Hello, so i’m trying to call a getter function i have inside one of my strucs (MyStruct.h) from another class (AnotherClass.cpp) and getting this error when trying to compile:



Error ....\Game\AnotherClass.cpp(72) : error C3867: 'FMyStruct::GetValueInSlot': non-standard syntax; use '&' to create a pointer to member



MyStruct.h



USTRUCT()
struct FMyStruct
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	TArray<int32> IntArr;

public:
	int32 GetValueInSlot(int32 Index) const
	{
		return IntArr[Index];
	}

	FMyStruct(int32 ArrSize)
	{
		IntArr.Init(0, ArrSize);
	}

}


AnotherClass.h



#include "MyStruct.h"

class AnotherClass {
	TArray<FMyStruct> SArr;
	int32 Index;
	FMyStruct CurrentStruct;
public:
	AnotherClass();
	int32 GetStructArrVal(int32 InIndex);
}


AnotherClass.cpp



AnotherClass::AnotherClass()
{
	SArr.Add(FMyStruct(1));
	SArr.Add(FMyStruct(2));
	SArr.Add(FMyStruct(3));

	CurrentStruct = SArr[0];
}

int32 AnotherClass::GetStructArrVal(int32 InIndex)
{
	return CurrentStruct.GetValInSlot(InIndex);
}



Any idea of what am i doing wrong? thanks in advance

  1. Not directly related to your problem, but careful with “TArray<FMyStruct> SArr;”. The type you pass as template parameter must conform to some specification (see documentation about TArray) such as " Makes the assumption that your elements are relocate-able; i.e. that they can be transparently moved to new memory without a copy constructor.". Also if later you add attributes to this class, this may create performance issues or even runtime issues depending on their type. If in doubt, use pointers instead such as “TArray<FMyStruct*> SArr;”
  2. the member function is named GetValueInSlot() but you are calling CurrentStruct.GetValInSlot()
  3. the following code compiles fine under VS2017. It is syntaxically correct but as mentioned in 1. it may not be optimal.


USTRUCT()
struct FMyStruct
{
	GENERATED_USTRUCT_BODY()

		UPROPERTY()
		TArray<int32> IntArr;

public:
	FMyStruct() {}
	int32 GetValueInSlot(int32 Index) const
	{
		return IntArr[Index];
	}

	FMyStruct(int32 ArrSize)
	{
		IntArr.Init(0, ArrSize);
	}
};


class AnotherClass {
	TArray<FMyStruct> SArr;
	int32 Index;
	FMyStruct CurrentStruct;
public:
	AnotherClass();
	int32 GetStructArrVal(int32 InIndex);
};


AnotherClass::AnotherClass()
{
	SArr.Add(FMyStruct(1));
	SArr.Add(FMyStruct(2));
	SArr.Add(FMyStruct(3));

	CurrentStruct = SArr[0];
}

int32 AnotherClass::GetStructArrVal(int32 InIndex)
{
	return CurrentStruct.GetValueInSlot(InIndex);
}


Thank you very much for the reply, ill set up a pointer as the type template parameter, im quite new to c++ and UE4, would you mind explaining this function declaration?



	FMyStruct() {}
	int32 GetValueInSlot(int32 Index) const
	{
		return IntArr[Index];
	}


what is the difference of that function declaration as opposed to this one? (is it just a default constructor declaration before the getter? if so, why?)



	int32 GetValueInSlot(int32 Index) const
	{
		return IntArr[Index];
	}


thanks in advance!

Default constructor was needed by VisualStudio 2017 C++ compiler (else error was generated)