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);
}
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 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?
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 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
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
there is no member name
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.
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.
But let’s not hijack the topic. It’s solved so yeah… Have a nice day! :]