I have have created a base storage class that can contain any time of class that’s based of of the Base Item
class.
I added generics to the base storage class and everything looked and compiled fine. After i tried to create a BagStorage
class that extends the base storage class. Compiling works fine as long as i don’t add any method. but after adding the constructor to the BagStorage
, even after leaving it empty, it generates errors after compiling. The bag storage is only allowed to hold a specific child of the base Item
class.
What am i doing wrong and how can i fix it?
EDIT
After playing around some, i got the code working by adding FORCEINLINE
to the constructor implementation in the cpp file. However… i have no clue why this works. Since i don’t think it’s the right way to place FORCEINLINE
Everywhere…
Base storage header
#pragma once
#include "CoreMinimal.h"
#include "Venandi/Item/Item.h"
template <class ItemType>
class TStorage
{
TArray<ItemType*> Storage;
int Space;
TArray<EItemType> AcceptableTypes;
public:
TStorage();
ItemType* Get(int Index);
TArray<ItemType*> GetAll() const;
void Add(ItemType* Item);
void Remove(int Index);
void Move(int From, int To);
int GetUsedSpace() const;
int GetAvailableSpace() const;
bool HasAvailableSpace() const;
int GetTotalSpace() const;
void SetTotalSpace(int Amount);
};
Based storage implementation
#include "Storage.h"
template <class ItemType>
TStorage<ItemType>::TStorage()
{
SetTotalSpace(1);
}
template <class ItemType>
void TStorage<ItemType>::Add(ItemType* Item)
{
Storage.Add(Item);
}
template <class ItemType>
void TStorage<ItemType>::Remove(const int Index)
{
Storage.RemoveAt(Index, 1, true);
}
Bag storage header
#pragma once
#include "CoreMinimal.h"
#include "Venandi/Inventory/Storage.h"
#include "Venandi/Item/Bag/Bag.h"
class TBagStorage : public TStorage<UBag>
{
TBagStorage();
};
Bag storage implementation
#include "BagStorage.h"
TBagStorage::TBagStorage()
{
SetTotalSpace(5);
}
Output log
1>------ Build started: Project: Venandi, Configuration: Development_Editor x64 ------
1>Using 'git status' to determine working set for adaptive non-unity build (E:\Unreal Projects\Venandi).
1>Invalidating makefile for VenandiEditor (UHT files changed)
1>Parsing headers for VenandiEditor
1> Running UnrealHeaderTool "E:\Unreal Projects\Venandi\Venandi.uproject" "E:\Unreal Projects\Venandi\Intermediate\Build\Win64\VenandiEditor\Development\VenandiEditor.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -installed
1>Reflection code generated for VenandiEditor in 4,6175181 seconds
1>Building VenandiEditor...
1>Using Visual Studio 2019 14.24.28316 toolchain (C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314) and Windows 10.0.18362.0 SDK (C:\Program Files (x86)\Windows Kits\10).
1>Building 19 actions with 12 processes...
1> [1/19] BagStorage.cpp
1> [2/19] ItemStorage.cpp
1> [3/19] Storage.cpp
1> [4/19] Wallet.gen.cpp
1> [5/19] InventoryComponent.cpp
1> [6/19] Bag.gen.cpp
1> [7/19] ItemType.cpp
1> [8/19] Bag.cpp
1> [9/19] Item.cpp
1> [10/19] Item.gen.cpp
1> [11/19] InventoryComponent.gen.cpp
1> [12/19] Wallet.cpp
1> [13/19] Venandi.init.gen.cpp
1> [14/19] ItemType.gen.cpp
1> [15/19] ItemRarity.gen.cpp
1> [16/19] ItemRarity.cpp
1> [17/19] UE4Editor-Venandi.lib
1> Creating library E:\Unreal Projects\Venandi\Intermediate\Build\Win64\UE4Editor\Development\Venandi\UE4Editor-Venandi.lib and object E:\Unreal Projects\Venandi\Intermediate\Build\Win64\UE4Editor\Development\Venandi\UE4Editor-Venandi.exp
1> [18/19] UE4Editor-Venandi.dll
1> Creating library E:\Unreal Projects\Venandi\Intermediate\Build\Win64\UE4Editor\Development\Venandi\UE4Editor-Venandi.suppressed.lib and object E:\Unreal Projects\Venandi\Intermediate\Build\Win64\UE4Editor\Development\Venandi\UE4Editor-Venandi.suppressed.exp
1>BagStorage.cpp.obj : error LNK2019: unresolved external symbol "public: __cdecl TStorage<class UBag>::TStorage<class UBag>(void)" (??0?$TStorage@VUBag@@@@QEAA@XZ) referenced in function "private: __cdecl TBagStorage::TBagStorage(void)" (??0TBagStorage@@AEAA@XZ)
1>BagStorage.cpp.obj : error LNK2019: unresolved external symbol "public: void __cdecl TStorage<class UBag>::SetTotalSpace(int)" (?SetTotalSpace@?$TStorage@VUBag@@@@QEAAXH@Z) referenced in function "private: __cdecl TBagStorage::TBagStorage(void)" (??0TBagStorage@@AEAA@XZ)
1>E:\Unreal Projects\Venandi\Binaries\Win64\UE4Editor-Venandi.dll : fatal error LNK1120: 2 unresolved externals
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.MakeFile.Targets(44,5): error MSB3075: The command "E:\UE_4.24\Engine\Build\BatchFiles\Build.bat VenandiEditor Win64 Development -Project="E:\Unreal Projects\Venandi\Venandi.uproject" -WaitMutex -FromMsBuild" exited with code 5. Please verify that you have sufficient rights to run this command.
1>Done building project "Venandi.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
AFTER EDIT:
#include "BagStorage.h"
FORCEINLINE TBagStorage::TBagStorage()
{
}