I am trying to figure out a good place to declare my inversion of control container.
I have placed it inside the main project header file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Engine.h"
#include "Runtime/Core/Public/Misc/TypeContainer.h"
TTypeContainer<> IOCContainer;
And inside my ATESTGameModeBase I am registering my classes:
ATESTGameModeBase::ATESTGameModeBase() {
IOCContainer.RegisterClass<IDebugHelper, UOnScreenDebugHelper>();
}
And where i need to use it, I am calling it like so:
auto debugHelper = IOCContainer.GetInstance<IDebugHelper>();
When i compile, i get the error:
Severity Code Description Project File Line Suppression
State Error LNK2005 “class
TTypeContainer<0> IOCContainer”
(?IOCContainer@@3V?$TTypeContainer@$0A@@@A)
already defined in
PCH.TEST.h.obj TEST G:\Programming\TEST\Intermediate\ProjectFiles\Module.TEST.cpp.obj 1
Where is a good place to place my IOC container? It obviously needs to be accessed from anywhere, but it looks like this common file is problematic? What’s common practice? I can find literally no examples, anywhere… at all. Search google for TTypeContainer, only the source engine test comes up.
I have been following: Engine/Source/Runtime/Core/Private/Tests/Misc/TypeContainerTest.cpp but this example does not provide much in terms of real life example.
For completeness, here is my interface:
#pragma once
// UE includes
#include "Runtime/Core/Public/Misc/TypeContainer.h"
//
#include "IDebugHelper.generated.h"
/**
*
*/
UINTERFACE()
class TEST_API UDebugHelper : public UInterface
{
GENERATED_BODY()
};
class IDebugHelper
{
GENERATED_BODY()
public:
virtual void WriteInfo() = 0;
};
// Expose_TNameOf(IDebugHelper) // If i add, there is an error: ')': Bad command or expression?
// I found out that this must be in the .cpp file
and here is my implementation:
#pragma once
// UE includes
// Project includes
#include "Abstracts/IDebugHelper.h"
//
#include "OnScreenDebugHelper.generated.h"
/**
*
*/
UCLASS()
class UOnScreenDebugHelper : public UObject, public IDebugHelper
{
GENERATED_BODY()
public:
void WriteInfo() override;
};