Hi! I’m new to UE4 interfaces and trying to implement a class that inherits from two interfaces. I’m posting the simplified code for which I’m getting Error: NoiseBase.gen.cpp.obj : error LNK2005: "public: int __cdecl INoiseBase::GetNoise(float,float,float,float,int) " (?GetNoise@INoiseBase@@QEBAHMMMMH@Z) already defined in NoiseBase.cpp.obj
This is the first interface and its NoisePattern.h
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "NoisePattern.generated.h"
class UStats;
UINTERFACE(BlueprintType)
class PROCEDURALWORLD_API UNoisePattern: public UInterface
{
GENERATED_BODY()
};
class PROCEDURALWORLD_API INoisePattern
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
int Calculate(UStats* stats, FVector position);
};
The NoisePattern.cpp file is empty it only has #include “NoisePattern.h”. My second interface class NoiseBase.h:
#pragma once
#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "NoiseBase.generated.h"
UINTERFACE(BlueprintType)
class PROCEDURALWORLD_API UNoiseBase : public UInterface
{
GENERATED_BODY()
};
class PROCEDURALWORLD_API INoiseBase
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
int GetNoise(float x, float y, float z, float scale, int32 max);
};
Now I wanted to implement the GetNoise default implementation, so this is NoiseBase.cpp:
#include "NoiseBase.h"
#include "SimplexNoise.h"
int INoiseBase::GetNoise(float x, float y, float z, float scale, int32 max)
{
return FMath::FloorToInt((SimplexNoise::noise(x * scale, y * scale, z * scale) + 1) * (max / 2.0f));
}
And finally the class that inherits both of the interfaces Frequency.h:
#pragma once
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "NoisePattern.h"
#include "NoiseBase.h"
class UStats;
class PROCEDURALWORLD_API Frequency: public INoisePattern, public INoiseBase
{
public:
UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
int Calculate(Stats* stats, FVector position);
virtual int Calculate_Implementation(Stats* stats, FVector position) override;
};
Frequency.cpp:
#include "Frequency.h"
#include "Stats.h"
int Frequency::Calculate_Implementation(Stats* stats, FVector position)
{
return GetNoise(noisePosition.X / 100, noisePosition.Y / 100, 0, stats->frequency, stats->elevation);
}
The NoisePattern functions normally and it successfully overrides it. The problem is this GetNoise function inside the Calculate_Implementation for which it says it has multiple definitions. Is it impossible to define the function inside the interface .cpp file?