🌈 UE5 C++ And Blueprint Community Tutorials (Notification Center)

(See All Tutorials)

Compiler Error: Cannot convert argument 1 from ‘UClass *’ to ‘TSubclassOf’

Cannot convert argument 1 from 'UClass *' to 'TSubclassOf<UObject>'

or

use of undefined type 'TSubclassOf<UObject>'

Solution: Add this to your header file (.h) for your class!

#include "UObject/Object.h"
#include "Templates/SubclassOf.h"

Explanation: With the new compiling rules to speed up compiling time, you have to include SubclassOf.h, and it has to be in your .h, not just in your .cpp.

It is quite common to include obviously, so you may not encounter this error if you are including other engine header files, until some unexpected time when you have about 5 min before your project’s demo, and you just wanted to do that one last compile :).

Also you might not encounter this error unless you are packaging a plugin using RunUAT, as I did not encounter this issue with a standard project compile, only when packaging the plugin independently.

:heart:

Rama

PS: Here is my whole .h file relevant portion if you want to replicate the compile error:

// Copyright Rama All Rights Reserved.

#pragma once

//!
#include "UObject/Object.h"
#include "Templates/SubclassOf.h"

#include "Kismet/BlueprintFunctionLibrary.h"
#include "VictoryBPFunctionLibrary.generated.h"

/*
	Victory to You! <3 Rama
*/
UCLASS()
class UVictoryBPFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_UCLASS_BODY()

	//~~~~~~~~~~~~~~~~~~~
	// 	  Load Object
	//~~~~~~~~~~~~~~~~~~~
	
	/** The FName that is expected is the exact same format as when you right click on asset -> Copy Reference! You can directly paste copied references into this node! IsValid lets you know if the path was correct or not and I was able to load the object. MAKE SURE TO SAVE THE RETURNED OBJECT AS A VARIABLE. Otherwise your shiny new asset will get garbage collected. I recommend you cast the return value to the appropriate object and then promote it to a variable :)  -Rama */
	UFUNCTION(BlueprintCallable, Category = "Victory BP Library|Misc", Meta = (DeterminesOutputType = "ObjectClass"))
	static UObject* LoadObjectFromAssetPath(TSubclassOf<UObject> ObjectClass, FName Path, bool& IsValid);

2 Likes