Why does one of my declarations produce an error, while its twin works fine?

I have several custom subclasses of ActorComponent that I’m trying to instantiate on my ACharacter at runtime. I’m using identical syntax to declare and initialize them, but one works perfectly and the other produces a compiler error.

Both classes are included in the header, and declared in the class dec:

#include "Combat.h"
#include "Perception.h"

UCLASS()
class LearningProject_API ACharacter : public ABaseCharacter
{
	GENERATED_BODY()
public:
	UCombat* characterCombat;
	UPerception* characterPerception;
}

UCombat*'s declaration works fine, but UPerception’s dec produces two sets of the same errors: “missing ; before *”, and “missing type specifier- int assumed”. I can make this error go away by changing the last line to this:

class UPerception* characterPerception;

But it weirds me out that identical syntax is producing different results, and I’d rather not proceed without understanding the root behavior.

I have the class constructor and everything as well, I just elided them from the sample as I assumed they weren’t relevant- if I included the entire character header, it would be hundreds of lines.

Hi,

seems your class are character based and i don’t sure why it’s miss some parts, when i did my class in 4.8.3 it’s

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

#pragma once

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

UCLASS()
class LIGHTARRAY_API AMyCharacter : public ACharacter
{
	GENERATED_BODY()

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

	// 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;

};

do you mean template code take extra space or there’re hundreds of error lines?

i can’t find UCombat’s declaration in installed engine and source code folders, are you sure you have such file?

this code builds without error

MyCharacter.h

#include "GameFramework/Character.h"
#include "MyActorComponent.h"
#include "MyCharacter.generated.h"

UCLASS()
class LIGHTARRAY_API AMyCharacter : public ACharacter
{
	GENERATED_BODY()

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

	// 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;

	UMyActorComponent* MyActorComponent_ref;
	
};

MyActorComponent.h

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

#pragma once

#include "Components/ActorComponent.h"
#include "MyActorComponent.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class LIGHTARRAY_API UMyActorComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UMyActorComponent();

	// Called when the game starts
	virtual void BeginPlay() override;
	
	// Called every frame
	virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;

};

can you show your classes declaration?

UCombat
UPerception

I mean that my character file is very long, and most of it is unrelated to the compiler error. UCombat and UPerception are classes I wrote, they are subclasses of UActorComponent proprietary to my system, not part of unreal’s API.

This looks like it might be a linker error. Did you include #pragma once on top of both of your header files?

Neither class has content yet, both simply contain the call to generated_body.

sry, but then i have no idea what cause problem in your implementation without looking into full version, as i told code i gave above builds without error, if you won’t share your code for some reason, then you can try one of these:

try recreate your classes without copying tons of lines, maybe just names, in this case you may find something different

or

try remove extra parts in your code step by step to reduce code down to raw template version and check when it builds without errors, from this point you can try find what’s wrong with last removed part

Thank you for trying to help anyway. :slight_smile:

Darn, I was hoping that was the mistake, but the #pragma is in place- both headers read like this:

#pragma once

#include "Morpheme.h"
#include "MCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "Perception.generated.h"

/**
 * 
 */
UCLASS()
class MONSTERSCHOOL_API UPerception : public UMorpheme
{
	GENERATED_BODY()


};

I think Bolt-Action Balrog is right. This is a linker error. If you by any chance come across:

// This gives errors
SomeClass Instance;

// And this doesnt give any error
class SomeClass Instance;

Then it means linker cant link your SomeClass because it can not find it. What you are achieving by adding class in front of it is forward declaration. So it still can not find but thinks you will create it at some point.

Check if you included the correct header for your class and be sure your class is properly created.

Can you copy-paste every error you get from the output?

Yea sure, if I re-add the #include"MCharacter.h", I get this in the log, where MCharacter.h(97) is the line where I declare a variable of type UPerception*:

Info Compiling game modules for hot reload
Info Parsing headers for MonsterSchoolEditor
Info LogCompile:Error: Circular dependency detected for filename C:\Users\username\Documents\Unreal Projects\projectname\Source\projectname\MCharacter.h!
Info Reflection code generated for MonsterSchoolEditor
Info Performing 7 actions (4 in parallel)
Info Perception.cpp
Info MCharacter.cpp
Info HitframeNotifyState.cpp
Info projectname.generated.cpp
Error c:\users\username\documents\unreal projects\projectname\source\ projectname \MCharacter.h(97)  : error C2143: syntax error : missing ';' before '*'
Error c:\users\ username \documents\unreal projects\ projectname \source\ projectname \MCharacter.h(97)  : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Info CharacterStateChangeNotify.cpp
Info TransitionZone.cpp

You’re both absolutely right, the line causing the problem was one of the includes:

#include "MCharacter.h"

MCharacter is my custom character class, I confirmed that it’s correctly spelled and that there’s no circular reference anywhere, is there any other obvious reason why this would break the linker?

Looks like a circular reference to me, especially if it works if you prefix the declaration with “class”.

If your MCharacter inherits from ACharacter, that means that MCharacter includes the character header (which includes Perception.h), while Perception.h also tries to include MCharacter.h.

To solve this, you need a forward declaration, which is precisely prefixing the declaration with class:

Here are my assumptions:

  1. You have a circular dependency like this:

    // SomeHeader
    #include “AnotherHeader.h”

    // AnotherHeader
    #include “SomeHeader.h”

My suggestions:

  • Restructure your both classes
  • Use forward-declaration

Both are fine but remember, circular dependency generally means, bad structered class hiearchy

  1. The line before MCharacter.h(97) is missing a ‘;’ at the end.

  2. At line 97 you have a type that is not declared yet, which is probably an effect of circular dependency

It could also be UnrealBuildTool detecting circular dependency and doing something on his own to solve this and causing the 3th problem.

glad to hear cut step by step work always ^^

i have an idea how it can happen:

  1. #include “MCharacter.h” mean this file must be in same folder as current where it wrote, isn’t here red underline on this include in VS? if no red underline, then it just strange
  2. i saw it’s possible to choose public or private when creating class in UE4 and in VS project have 2 separate folders, i think if your MCharacter.h inside “private” folder for example and another .h where you try include it from “public” folder - then VS can’t just find where the hell file is :smiley: and for some reason doesn’t rise correct error
  3. if you were deleting files from VS solution, note that files maybe not deleted physicly on HDD and exist in window’s file explorer, in this case you may get strange situation when you have actually 2 files MCharacter.h inside “public” and “private” folder and if your file where you include it lay in wrong folder, tricky VS can find it on HDD and think all fine, while it’s totaly wrong and you want use really another one

just a remind - don’t forget mark question as answered with little gray circle button under any answer (not comment, but whole answer) when problem solved, so anyone else later can have same question and may find solution faster, if you find solution on your own, don’t forget write it too, really interesting who’s bad guy in this problem :stuck_out_tongue:

it’s already like detective movie for me xD

Hmm okay, I’ll take a look at restructuring… thank you so much for the analysis :slight_smile: