Working with interfaces, multiple definition error

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?

Hmm… first, although I’ve never tried or looked, I think if you’re using the unreal interfaces your class that implements them needs to have UCLASS() definition that Frequency is missing. Also, Frequency says it implements INoiseBase but doesn’t implement GetNoise().

well I tried with UCLASS and without UCLASS before and it didnt make any difference
the thing is i dont want the Frequency class to implement it, i want it to recognize and use the default implementation from INoiseBase

Ok, well I think the error you’re getting is because INoiseBase::GetNoise() is flagged as BlueprintNativeEvent so the iNoiseBase::GetNoise() implementation is autogenerated in the .generated.h header and then you then try to reimplement in the .cpp. It should be INoiseBase::GetNoise_Implementation() there. BUT… aren’t interfaces pure abstract? So I suspect you are going to have problems afterwards anyway.

If the intention is GetNoise() to just be a helper function that’s not implementable and won’t change, just implement it as a static function of UNoisePattern otherwise you’ll have to implement it in any class the implements the interface.

thanks so much for your help