Announcement

Collapse
No announcement yet.

Error when compiling code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Error when compiling code

    I get that error when trying to compile my code. this is the error
    "CompilerResultsLog: Error: C:/Users/Me/Documents/Unreal Projects/cpp_learn/Source/cpp_learn/MyActor.h(5) : Error: #include found after .generated.h file - the .generated.h file should always be the last #include in a header".
    This is my code:
    "MyActor.h"
    #pragma once

    #include "GameFramework/Actor.h"
    #include "OnComponentHit.generated.h"
    #include "MyActor.generated.h"

    class UBoxComponent;

    UCLASS()
    class UNREALCPP_API AOnComponentHit : public AActor
    {
    `GENERATED_BODY()`

    public:
    `// Sets default values for this actor's properties`

    `AOnComponentHit();`

    `UPROPERTY(VisibleAnywhere)`

    `class UBoxComponent* MyComp;`

    `UFUNCTION()`

    `void OnCompHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit);`
    };

    "MyActor.cpp"
    #include "OnComponentHit.h"
    #include "Components/BoxComponent.h"


    // Sets default values
    AOnComponentHit::AOnComponentHit()
    {
    `// Use a sphere as a simple collision representation`

    `MyComp = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComp"));`

    `MyComp->SetSimulatePhysics(true);`

    `MyComp->SetNotifyRigidBodyCollision(true);`

    `MyComp->BodyInstance.SetCollisionProfileName("BlockAllDynamic");`

    `MyComp->OnComponentHit.AddDynamic(this, &AOnComponentHit::OnCompHit);`

    `// Set as root component`

    `RootComponent = MyComp;`
    }

    void AOnComponentHit::OnCompHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
    {
    `if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))`

    `{`

    `if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("I Hit: %s"), *OtherActor->GetName()));`

    `}`
    }

    #2
    The error message is already telling you what's wrong...
    ".generated.h" must always be the last include (when there's UCLASS in that header).

    And you cannot include "generated.h" from another header either.
    | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

    Comment

    Working...
    X