Passing a struct through a function template

Hey all,

I’m trying to sort out a template method for grabbing CSV data and importing it into global classes for access from other classes. And I’m stuck.

First, here’s my .h template code:

template<typename Y, typename T>
	void InitSpecificDatabase(UDataTable* DataTable, T& str, TArray<Y>& arr)
	{
		//Let's init our database.
		if (DataTable != NULL)
		{
			for (auto it : DataTable->RowMap)
			{
				str* strdata = (str*)(it.Value);
				arr.Add(*strdata);
			}
		}
	}

And here’s the .cpp code that accesses it:

UDataTable* DataTable;

FYieldData* YieldData;
DataTable = LoadObjFromPath(TEXT("DataTable'/Game/CoreData/YieldData.YieldData'"));
InitSpecificDatabase(DataTable, YieldData, m_aYieldTypes);

With this, I get the following error:

error C2065: 'strdata': undeclared identifier

So, my question: is it possible to pass the struct type through the template in order to have properly identify as the struct element for str*?

Thanks!
LG

You are trying to define str as a type while ti really is just a variable name. The type is T and you must use that when dereferencing it. That being said, why do you need to dereference it?

HTH

Thanks for the response. I’m having difficulty getting the type T to grab the proper values out of the CSV values without an explicit declaration of the struct (T by itself isn’t working). This was my latest attempt, hoping that a dereference would work, but it is not. This is my first experience with unreal’s datatables so I’m unsure of a proper solution to the problem, thanks!

You have to specify what type T is then. Instead of generic types, just make it a parent type. Then it works fine.

Nevermind, I see what you are saying now. using T& works (in lieu of str). Thanks!

Then please accept the answer by clicking the checkmark.

Good luck with your game!