Error C2227: left of '->SpawnActor' must point to class/struct/union/generic type UE4

I created maze generation lib, but when I try to compile, it returns error:

CompilerResultsLog: Error: D:\Home\Volodya\Projects\Games\UE4\TestingSomething\Something\Source\Something\Private\MazeLib.cpp(17) : error C2352: 'UObject::GetWorld': illegal call of non-static member function
CompilerResultsLog: Error: C:\Program Files\Epic Games\UE_4.18\Engine\Source\Runtime\CoreUObject\Public\UObject/Object.h(473) : note: see declaration of 'UObject::GetWorld'
CompilerResultsLog: Error: D:\Home\Volodya\Projects\Games\UE4\TestingSomething\Something\Source\Something\Private\MazeLib.cpp(17) : error C2227: left of '->SpawnActor' must point to class/struct/union/generic type
CompilerResultsLog: Error: D:\Home\Volodya\Projects\Games\UE4\TestingSomething\Something\Source\Something\Private\MazeLib.cpp(17) : error C2227: left of '->GetAuthoritativeClass' must point to class/struct/union/generic type
CompilerResultsLog: ERROR: UBT ERROR: Failed to produce item: D:\Home\Volodya\Projects\Games\UE4\TestingSomething\Something\Binaries\Win64\UE4Editor-Something-5058.dll

My spawnMaze function in cpp file:

void UMazeLib::spawnMaze(int size) {
	bool** maze = generateMaze(size);
	for (int h = 0; h < size; h++)
	{
		maze[h] = new bool[size];
		for (int w = 0; w < size; w++)
		{
			const FVector* position = new FVector(10 * h, 10 * w, 0);
			GetWorld()->SpawnActor(wallClass->GetAuthoritativeClass(), position);
		}
	}
}

and my .h file:

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Wall.h"
#include "MazeLib.generated.h"

/**
 * 
 */
UCLASS()
class SOMETHING_API UMazeLib : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
	public:	
		UFUNCTION(BlueprintCallable, Category = "Something")
		static void spawnWall(FTransform str);

		UFUNCTION(BlueprintCallable, Category = "Maze")
		static void spawnMaze(int size);

		UPROPERTY(BlueprintReadWrite, EditAnywhere)
		TSubclassOf<class AWall> wallClass;

		UPROPERTY(BlueprintReadWrite, EditAnywhere)
		UWorld* world = GetWorld();

	private:
		static	bool** generateMaze(int size);
};

A static function has no “this” object so you can’t call GetWorld(). You either need to make it non-static or pass in the actor pointer as a parameter.

Yeah, but when I make function spawnMaze() non-static, it’s hiding in level Blueprint.

Yes, I only remove ‘static’ from declaration.

That should be possible. What does your code look like? You just removed the static keyword from the function declaration?

Oh it’s a function library. For some reason I thought it was a level BP. I don’t have much experience with function libraries, but if it has to be static then just pass the world as a function parameter instead of calling GetWorld() inside the function. When you call the function, you can pass in GetWorld().

So, I have to add world object as function parameter. But why I can’t use non-static methods threre. So my function must be like this: static void spawnMaze(int size, UWorld* world)?

You can’t use non-static members from a static function. You need an object to work with. There’s no way I can explain it better than the references available online. I found this one, for example

And yes, that function should work. It looks like you have to use static functions so passing in parameters is the only way to go.

Did it work out for you? If not, let me know. If so, please close the question (check-mark under the up/down-vote widget). By the way, if at any time one of your parameters is an AActor then you can call GetWorld() from that (and save an extra parameter).