Object finder: cannot convert 'UStaticMesh ' to 'UObject '

The error in the output log:

error C2664: 'void
ConstructorHelpers::ValidateObject(UObject
*,const FString &,const TCHAR *)': cannot convert argument 1 from
'UStaticMesh *' to 'UObject *'
Line 109 in ConstructorHelpers.h: ValidateObject( Object, PathName, ObjectToFind );

The code in the constructor:

	static ConstructorHelpers::FObjectFinder<UStaticMesh>meshAsset(TEXT("/Game/Meshes/SM_MinePickaxeB_01")); // <<< error for this line
StaticMeshComp->SetStaticMesh(meshAsset.Object);

I also tried my own function but that one gave the exact same error:

// cpp constructor call
StaticMeshComp->SetStaticMesh(ULib::CreateObject<UStaticMesh>("/Game/Meshes/SM_MinePickaxeB_01"));

// static function

	template <typename Type>
	static Type* CreateObject(FString Path)
	{
		Type* Temp = nullptr;
		Temp = ConstructorHelpers::FObjectFinderOptional<Type>(*Path).Get();

        #if WITH_EDITOR
		  if (Temp == nullptr) { UE_LOG(LogTemp, Error, TEXT("%s"), *Path) }
        #endif

		return Temp;
	}

It compiled before but I added literally 1 line to the cpp file:

WeaponAnimType = EWeaponAnimType::Pistol; // TODO: Use pickaxe animation.

and ever since I can’t get it to compile anymore… Not even when I outcomment that newly added line. What am I doing wrong?

So eventually I outcommented it all in a desperate way to get the project to compile and then this happened:

That’s literally 3 compiler errors in a row for 1 (outcommented) line of code. That’s really bad…

I cleaned the project AGAAAAAAAAAAAIN and now it compiles. Of course without any mesh assigned but hell, at least the project compiles. What is going on…

Apparently the compiler expects an UObject. UStaticMesh* is an object but… Pressing F12 on UStaticMesh* brings me to a forward declaration in Engine.h. So the compiler does not (yet) know that this in fact is an UObject.

Solution (yes it’s very simple):

#include "Engine/StaticMesh.h"

Of course the question still remains: Why did it compile prior to adding this nonsense line:

 WeaponAnimType = EWeaponAnimType::Pistol; // TODO: Use pickaxe animation.

And why does it also not compile anymore when I hit ctrl+z until it reverts back to the original state? It still does not compile even though the code is now EXACTLY THE SAME as when it used to compile. BUT when I use SourceControl to revert the exact same change, then it compiles. So even though I found the problem and solved it, there is something much more nasty going on somewhere (and I still suspect a UE4-related bug). If someone figures this out some day, please let me know!

I spent a day trying to solve this problem. Error message is not informative and Visual Studio doesn’t warn me about adding that header file. Now it works, thank you!