Proper return value declaration for a C++ function

Definitely a newbie question here…

I attempted a C++ class for my Actor. Here is the header file:

USTRUCT(BlueprintType)
struct FPointCloudPoint
{
	GENERATED_BODY()

	FVector Vector;
	FColor Color;
};

UCLASS()
class POINTCLOUDPRACTICE_API APointCloudScript : public AActor
{
	GENERATED_BODY()

public:
	APointCloudScript();
protected:
	virtual void BeginPlay() override;
public:
	virtual void Tick(float DeltaTime) override;

	UFUNCTION(BlueprintCallable, Category = "Make Point Cloud")
		TArray<FPointCloudPoint> GeneratePointCloud();
};

And here is the function in source file:

TArray<APointCloudScript::FPointCloudPoint> APointCloudScript::GeneratePointCloud()
{
    TArray<FPointCloudPoint> PointCloudArray;

    const int32 NumPoints = 100;
    FRandomStream RandomStream;

    for (int32 i = 1; i <= NumPoints; i++)
    {
         //The rest of the code here that creates a random vector and color...//

        FPointCloudPoint Point;
        Point.Vector = ResultVector;
        Point.Color = RandomColor;
        //Append to array
          PointCloudArray.Add(Point);
    }

    return PointCloudArray;
}

Basically what I want to happen is for this GeneratePointCloud function to return an array of struct PointCloudPoint. But I am getting an error:

error C2039: ‘FPointCloudPoint’: is not a member of ‘APointCloudScript’

which do not makes sense to my newbie brain. I would appreciate any help!

Hi, xianth ryllis

  1. U didnt initialized “ResultVector” and “RandomColor” in method in source file. I dont understand how visual studio compiled this (or live coding)
  2. Add “UPROPERTY(BlueprintReadWrite)” to each field in your struct (it is necessary if u want to break or to make struct in blueprints)

Hello, thank you for your reply. Here is the copy of the entire method:

// Function to generate a point cloud with random points
TArray<APointCloudScript::FPointCloudPoint> APointCloudScript::GeneratePointCloud()
{
    TArray<FPointCloudPoint> PointCloudArray;

    const int32 NumPoints = 100;
    FRandomStream RandomStream;

    for (int32 i = 1; i <= NumPoints; i++)
    {
        // Step 1: Create a random unit vector
        float RandomX = RandomStream.FRandRange(-1.0f, 1.0f);
        float RandomY = RandomStream.FRandRange(-1.0f, 1.0f);
        float RandomZ = RandomStream.FRandRange(-1.0f, 1.0f);
        FVector RandomVector(RandomX, RandomY, RandomZ);
        RandomVector.Normalize();

        // Step 2: Get a random float to vary the scale of the vector
        float RandomFloat = RandomStream.FRandRange(10.0f, 500.0f);

        // Step 3: Multiply the vector and the float
        FVector ResultVector = RandomVector * RandomFloat;

        // Step 4: Add a random color
        uint8 RandomR = RandomStream.RandRange(0, 255);
        uint8 RandomG = RandomStream.RandRange(0, 255);
        uint8 RandomB = RandomStream.RandRange(0, 255);
        FColor RandomColor(RandomR, RandomG, RandomB, 255);

        // Step 5: Create a PointCloudPoint and append it to the array
        FPointCloudPoint Point;
        Point.Vector = ResultVector;
        Point.Color = RandomColor;
        PointCloudArray.Add(Point);
    }

    return PointCloudArray;
}

I had them declared on these lines:
FVector RandomVector(RandomX, RandomY, RandomZ);
and
FColor RandomColor(RandomR, RandomG, RandomB, 255);

or should I have had also declared them somewhere else in the code?

As for point number 2, it is not necessary because I would be needing to edit them in the blueprints, but I’m going to add UPROPERTY if it’s gonna make the whole thing compile and work

Add “UPROPERTY(BlueprintReadWrite)” in struct fileds and check if it will solve the problem

Yes, already did:
image

I still get the same error:
error C2039: ‘FPointCloudPoint’: is not a member of ‘APointCloudScript’

TArray<APointCloudScript::FPointCloudPoint> APointCloudScript::GeneratePointCloud()
Look at this definition. Ur function return array of struct “FPointCloudPoint” and u say that class owns this struct but it is wrong.

“APointCloudScript::FPointCloudPoint” means that ur class owns this struct. Look at header and u will see that struct is independent.

This is what visual studio says. What IDE do you use?
delete

Hello, thank you for replying. I am also using Visual Studio.

So I put my struct under the class APointCloudScript. Here’s how it looks:

header:

source file:

I still get the error: : Error: Unable to find ‘class’, ‘delegate’, ‘enum’, or ‘struct’ with name ‘FPointCloudPoint’

the error is called on the header file on the line:
TArray<APointCloudScript::FPointCloudPoint> APointCloudScript::GeneratePointCloud();

Please let me know if I understood you correctly.

I m not sure how legal is to use ustruct in uclass but the next code works

Header:

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PointCloudScript.generated.h"

USTRUCT(BlueprintType)
struct FPointCloudPoint
{
	GENERATED_BODY()

	UPROPERTY(BlueprintReadWrite)
	FVector Vector;
	UPROPERTY(BlueprintReadWrite)
	FColor Color;
};

UCLASS()
class CASTLEASSAULT_API APointCloudScript : public AActor
{
	GENERATED_BODY()
	
public:	
	UFUNCTION(BlueprintCallable, Category = "Make Point Cloud")
	TArray<FPointCloudPoint> GeneratePointCloud();

};

Source:

#include "PointCloudScript.h"

TArray<FPointCloudPoint> APointCloudScript::GeneratePointCloud()
{
    TArray<FPointCloudPoint> PointCloudArray;

    const int32 NumPoints = 100;
    FRandomStream RandomStream;

    for (int32 i = 1; i <= NumPoints; i++)
    {
        // Step 1: Create a random unit vector
        float RandomX = RandomStream.FRandRange(-1.0f, 1.0f);
        float RandomY = RandomStream.FRandRange(-1.0f, 1.0f);
        float RandomZ = RandomStream.FRandRange(-1.0f, 1.0f);
        FVector RandomVector(RandomX, RandomY, RandomZ);
        RandomVector.Normalize();

        // Step 2: Get a random float to vary the scale of the vector
        float RandomFloat = RandomStream.FRandRange(10.0f, 500.0f);

        // Step 3: Multiply the vector and the float
        FVector ResultVector = RandomVector * RandomFloat;

        // Step 4: Add a random color
        uint8 RandomR = RandomStream.RandRange(0, 255);
        uint8 RandomG = RandomStream.RandRange(0, 255);
        uint8 RandomB = RandomStream.RandRange(0, 255);
        FColor RandomColor(RandomR, RandomG, RandomB, 255);

        // Step 5: Create a PointCloudPoint and append it to the array
        FPointCloudPoint Point;
        Point.Vector = ResultVector;
        Point.Color = RandomColor;
        PointCloudArray.Add(Point);
    }

    return PointCloudArray;
}

1 Like

Cool! It worked! Thank you sooooo much! :smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.