Pointer to incomplete class type is not allowed

I’m having a problem with my code so I made a uboxcomponet class so when I go in to my .cpp file and try to use the pointer to incomplete class type is not allowed comes up and I tried doing the component/ubox.h and it still gives me the same error

.h file

#include "Core.h"
#include "GameFramework/Actor.h"
#include "pickup.generated.h"

UCLASS()
class HORDE_API Apickup : public AActor
{
	GENERATED_BODY()

		class UBoxComponent;


public:	
	// Sets default values for this actor's properties
	Apickup();

	UPROPERTY(editanywhere, Category = "Ammo Crate")
	UStaticMeshComponent *pickupmesh;
	

	

protected:
	UPROPERTY(editanywhere, Category = "Ammo Crate")
		UBoxComponent  *pickupbox;

	UPROPERTY(editanywhere, Category = "Ammo Crate")
	USceneComponent *pickuproot;

	UFUNCTION()
	void OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
	

	UPROPERTY(editanywhere, Category = "Ammo Crate")
		int32 count;


protected:
	// Called when the game starts or when spawned
		virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	
	
};

.ccp file

#include "pickup.h"
#include "hordeCharacter.generated.h"
#include "Components/StaticMeshComponent.h"

// Sets default values
Apickup::Apickup()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;


	pickuproot = CreateDefaultSubobject<USceneComponent>(TEXT("pickuproot"));
	RootComponent = pickuproot;

	pickupmesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("pickupmesh"));
	pickupmesh->AttachToComponent(pickuproot, FAttachmentTransformRules::SnapToTargetIncludingScale);

	pickupbox = CreateDefaultSubobject<UBoxComponent>(TEXT("pickupbox"));
    pickupbox->

}

void Apickup::OnOverlapBegin(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult & SweepResult)
{

}

// Called when the game starts or when spawned
void Apickup::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void Apickup::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

I add it and it still doesn’t work

C:\Program Files\Epic Games\UE_4.17\Engine\Source\Runtime\CoreUObject\Public\UObject/Object.h(83) : error C2027: use of undefined type ‘Apickup::UBoxComponent’

c:\users\flyea\onedrive\documents\unreal projects\horde\source\horde\pickup.h(14) : note: see declaration of 'Apickup::UBoxComponent'

C:\Users\flyea\OneDrive\Documents\Unreal Projects\horde\Source\horde\pickup.cpp(21) : note: see reference to function template instantiation 'TReturnType *UObject::CreateDefaultSubobject<Apickup::UBoxComponent>(FName,bool)' being compiled




C:\Program Files\Epic Games\UE_4.17\Engine\Source\Runtime\CoreUObject\Public\UObject/Object.h(84) : error C2440: 'static_cast': cannot convert from 'UObject *' to 'Apickup::UBoxComponent *'






C:\Program Files\Epic Games\UE_4.17\Engine\Source\Runtime\CoreUObject\Public\UObject/Object.h(84) : note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


here is after I compile

I did just did now I get all these errors

I just did it*

Hello.

You are missing include in your cpp file. Just add this and it should work:

#include "Components/BoxComponent.h"

The code still won’t compile tho, due to 19th line of your cpp file.
You’d have to either finish what you wanted to do there or remove it,

Also, you shouldn’t include generated.h files within your cpp files.

Cheers.

Could you show the content of your output log when you try to compile it?
Also, make sure you’ve read my edited answer.

Hello…

why your forward declaration is inside the class?

#include "Core.h"
#include "GameFramework/Actor.h"
#include "pickup.generated.h"
 
UCLASS()
class HORDE_API Apickup : public AActor
{
     GENERATED_BODY()
 
         class UBoxComponent;
 
public:    
     // Sets default values for this actor's properties
     Apickup();

That should be outside/before of the class

#include "Core.h"
#include "GameFramework/Actor.h"
#include "pickup.generated.h"

class UBoxComponent;

UCLASS()
class HORDE_API Apickup : public AActor
{
    GENERATED_BODY()

public:    
     // Sets default values for this actor's properties
     Apickup();

PS: this will work only if you follow Atheist advice about includes :wink:

I tend to use inlined forward declaration (like class UBoxComponent* pickupbox;) etc.
I’m guessing that in such case that UBoxComponent is still declared in scope of whatever class it is declared in (so it would be like APickup::UBoxComponent in this case) . So it doesn’t actually matter whether it’s declared inside or outside of that class. I think?

now it’s working

what I put is class uboxcomponet* pickupbox and it worked

So what? Using inlined forward declaration doesn’t declare a class within another class? O_o

@Down
I see it doesn’t work. I’m more like cursios about why it doesn’t work. Anyway… nvm.

Atheist… you have right…

but he just put this inside the class “class UBoxComponent;”
without any variable name, without anything… compiler try to understand this as variable…

class UBoxComponent* VariableName; works… of course… in this case no matter it is inside or outside :slight_smile: i mean inlined forward declaration also works, traditional forward declaration outside of the class also works…

he just tried to do traditional forward declaration inside the class instead of inlined :slight_smile:

no if he do not give name for that variable sigh… :smiley:

this will not work class UBoxComponent; inside the class

this will work class UBoxComponent YOURVARIABLENAME;* inside the class

@up

sry i missunderstod… http://jatinganhotra.com/blog/2012/11/25/forward-class-declaration-in-c-plus-plus/

because if you do that with a simple class xyclass and close with ; inside your class - that will be member instead of declaration and compiler try to understand as class member and will throw compile error… because

  1. there is no member name
  2. compile do not recognize class.

with usage of class xyclass pointertoclass;* you create an incomplete class pointer and compile knows that pointer is a forward declared and will not throw any error…

basically

class xyclass;

class yourclass
{
    xyclass* pointer;
}

same with

class yourclass
{
   class xyclass* pointer;
}

compiler understand both case, since class member pointing to class which is not included. :slight_smile:

Yeah, I get that. It still doesn’t answer my question tho. And the link you’ve provided explains when you can or cannot use type that’s already forward declared. Not in what scope that type actually exists. :stuck_out_tongue:

But let’s not hijack the topic. It’s solved so yeah… Have a nice day! :]