Can't add class! (Something wrong with my code?)

Here’s what happens whenever I try adding a class:


This didn’t happen with the previous class that I added. Looking at the error logs, it seems it’s something related to the code of the character I added before.

Visual studio returns no errors whatsoever, and when I try compiling in UE4 it always says “compiled successfully”.

Also, once this error pops up, the compile button disappears, and I have no way of getting it back until I restart. A bit of help here too please?

Here’s my code:

character.h:

#pragma once

#include "GameFramework/Character.h"
#include "SCharacter.generated.h"

UCLASS()
class SURVIVALGAME_API ASCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	ASCharacter();

	// Called when the game starts or when spawned
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void Tick( float DeltaSeconds ) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;

	// Movement //
	virtual void MoveForward(float Val);
	virtual void MoveRight(float Val);

	// Jump //
	void OnStartJump();
	void OnStopJump();

	// Health //
	UPROPERTY(EditDefaultsOnly, Category = "PlayerCondition", Replicated)
		float Health;
	UFUNCTION(BlueprintCallable, Category = "PlayerCondition")
		float GetMaxHealth() const;
	
};

character.cpp:

// Fill out your copyright notice in the Description page of Project Settings.

#include "SurvivalGame.h"
#include "SCharacter.h"

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

}

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

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

}

// Called to bind functionality to input
void ASCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);

	// Looking around //
	InputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
	InputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);

	// Movement //
	InputComponent->BindAxis("MoveForward", this, &ASCharacter::MoveForward);
	InputComponent->BindAxis("MoveRight", this, &ASCharacter::MoveRight);

	// Jump //
	InputComponent->BindAction("Jump", IE_Pressed, this, &ASCharacter::OnStartJump);
	InputComponent->BindAction("Jump", IE_Released, this, &ASCharacter::OnStopJump);

}

Finally, here’s the error that UE4 feeds me: [pastie.org/10234607][2]

I believe that’s about it… Thank you so much for your help!

The problem is you have not defined all of your functions in you CPP.

Linker errors are a bit horrid to read.

But if we take a look at the third one from your pastie link:

CompilerResultsLog:Error: Error SCharacter.cpp.obj : error LNK2001: unresolved external symbol "public: virtual void __cdecl ASCharacter::MoveForward(float)" (?MoveForward@ASCharacter@@UEAAXM@Z)

The .obj file is what contains the compiled code for your .cpp file. The .h effectively works as a declaration file, so other source files know what functions and variables your class has. When the linker links all of these seperate obj files into your game’s dll file. It tries to link the declaration for the MoveForward(float) function to the compiled code in the obj file. But there is no MoveForward function declare. Hence it can’t link it and fails.

Implement all of your declared functions, even if they just have empty “{ }” bodies should fix it.

Hi! Thanks for your reply! What do you mean there is no MoveForward function to declare? Where would I “create” this MoveForward function? I’m looking at other code as reference, and it seems nearly, if not, identical to what I have there.

Thanks again!

So in your header file you have a function declaration for BeginPlay:

virtual void BeginPlay() override;

In your .cpp file you have a definition for BeginPlay:

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

But your other functions, e.g. MoveForward do nott have any definitions. Atleast not in the example you copied and pasted.

In your .cpp you would have to add a definition that matches the declaration:

void ASCharacter::MoveForward(float Val)
{
    //Your code goes here.
}