# How to fix a Syntax Error on variable declaration of custom class?

**URL:** <https://forums.unrealengine.com/t/how-to-fix-a-syntax-error-on-variable-declaration-of-custom-class/311464>\
**Category:** Editor Scripting\
**Tags:** Variable, Class, syntax, unreal-engine, question, CPP\
**Created:** [March 18, 2015, 12:04am UTC](https://forums.unrealengine.com/t/how-to-fix-a-syntax-error-on-variable-declaration-of-custom-class/311464 "2015-03-18T00:04:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erdrik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Erdrik](https://forums.unrealengine.com/u/Erdrik)\
**Post date:** [March 18, 2015, 12:04am UTC](https://forums.unrealengine.com/t/how-to-fix-a-syntax-error-on-variable-declaration-of-custom-class/311464/1 "2015-03-18T00:04:52Z")

</div>

Im trying to declare a **ADMagicCharacter** variable in **Spell.h** , as seen below.

IntelliSense shows everything is fine, but during compile it throws a syntax error at _ADMagicCharacter Caster;_\*

" error C2143: syntax error : missing ‘;’ before ‘\*’ "

I can’t figure out the problem, as It appears that everything is correct?  
Please let me know if Im missing something, as Im at a loss…

**Spell.h**

```
#pragma once

#include "GameFramework/Actor.h"
#include "DMHeaderList.h"
#include "Spell.generated.h"

...

UCLASS()
class DMAGIC_API ASpell : public AActor
{
	GENERATED_BODY()
	
public:	
	
	// Sets default values for this actor's properties
	ASpell(const FObjectInitializer& ObjectInitializer);

...
	ADMagicCharacter* Caster;

...
	UPROPERTY(VisibleAnywhere, Category=Spells)	AProjectile* SpellProjectile;

...

};

```

**DMagicCharacter.h**

```
#pragma once

#include "GameFramework/Character.h"
#include "DMHeaderList.h"
#include "DMagicCharacter.generated.h"

...

UCLASS(config=Game)
class ADMagicCharacter : public ACharacter
{
	GENERATED_BODY()

...

public:
	ADMagicCharacter(const FObjectInitializer& ObjectInitializer);

...

};

```

**DMHeaderList.h**

```
#pragma once
#include "Engine.h"
#include "DMagicGameMode.h"
#include "DMagicCharacter.h"
#include "Spell.h"
#include "Projectile.h"

```

---

<div class="post-metadata">

**Author:** ![Crzyhomer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crzyhomer](https://forums.unrealengine.com/u/Crzyhomer)\
**Post date:** [March 18, 2015, 12:52am UTC](https://forums.unrealengine.com/t/how-to-fix-a-syntax-error-on-variable-declaration-of-custom-class/311464/2 "2015-03-18T00:52:45Z")

</div>

While it may seem like you DMHeaderList.h has the right ordering, the error seems to be that at the time Spell.h is compiled it doesn’t understand what ADMagicCharacter is.

It is probably because of the circular dependency in your headers. DMHeaderList.h includes Spell.h which includes DMHeaderList.h.

In this case you can forward declare your used of the ADMagicCharacter. Simple add the word “class” in front of your member variable, or better yet put “class ADMagicCharacter” right below your include files and above the class declaration.

Additionally, something to remember, is ALWAYS use UPROPERTY above UObject member variables or you will have garbage collection issues. Your Caster variable will be set to null on the first opportunity to garbage collect and you’ll crash if you access it. Or at least you will get bad behavior.

**Spell.h**

```
#pragma once

#include "GameFramework/Actor.h"
#include "Spell.generated.h"

...
// Forward declarations here (as long as its pointer type or const& and you don't need the internal details this will be fine)
// any time you use the implementation of the class, put it in the cpp and include DMagicCharacter.h there 
class ADMagicCharacter;
class AProjectile;

UCLASS()
class DMAGIC_API ASpell : public AActor
{
	GENERATED_BODY()
	
public:	
	
	// Sets default values for this actor's properties
	ASpell(const FObjectInitializer& ObjectInitializer);

...
	UPROPERTY()
    ADMagicCharacter* Caster;

...
	UPROPERTY(VisibleAnywhere, Category=Spells)
AProjectile* SpellProjectile;

...

};

```

**DMagicCharacter.h**

```
#pragma once

#include "GameFramework/Character.h"
#include "DMagicCharacter.generated.h"
//try to only include what you use  
//DMHeaderList.h is going to get too big and cause you future headache

...

UCLASS(config=Game)
class ADMagicCharacter : public ACharacter
{
	GENERATED_BODY()

...

public:
	ADMagicCharacter(const FObjectInitializer& ObjectInitializer);

...

};

```

**DMHeaderList.h**

```
#pragma once
#include "Engine.h"
#include "DMagicGameMode.h"
#include "DMagicCharacter.h"
#include "Spell.h"
#include "Projectile.h"
```

---

<div class="post-metadata">

**Author:** ![Erdrik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Erdrik](https://forums.unrealengine.com/u/Erdrik)\
**Post date:** [March 18, 2015, 1:02am UTC](https://forums.unrealengine.com/t/how-to-fix-a-syntax-error-on-variable-declaration-of-custom-class/311464/3 "2015-03-18T01:02:49Z")

</div>

Thank you, adding class in front of the **ADMagicCharacter** declaration solved the issue.  
Will circular dependency mess with the project? (aside from syntax errors like this. I mean at run time)

---

<div class="post-metadata">

**Author:** ![Crzyhomer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crzyhomer](https://forums.unrealengine.com/u/Crzyhomer)\
**Post date:** [March 18, 2015, 1:13am UTC](https://forums.unrealengine.com/t/how-to-fix-a-syntax-error-on-variable-declaration-of-custom-class/311464/4 "2015-03-18T01:13:46Z")

</div>

Circular dependencies will always result in a compile error of some kind. The issue is that the compiler is looking for the complete definition of something before it can consider it compilable. If it has to go figure out the definition of something else to understand part of the first thing and that second thing is defined in terms of the first, you can’t resolve that.

Here is a link on the concept if you would like more information

- [Stack Overflow](http://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration)
- [Herb Sutter’s code blog](http://herbsutter.com/2013/08/19/gotw-7a-solution-minimizing-compile-time-dependencies-part-1/)

It may not be that obvious as that, and in fact just using includes can run the compiler in circles, as was your case.

**A.h**

```
#include "B.h"
class A
{
   public:

   B* MyB;
};

```

**B.h**

```
#include "A.h"
class B
{
  public:
  A* MyA;
};

```

As defined, and pardon the bad pseudocode, the compiler could never resolve this because it would just bounce back and forth. One of them has to be resolveable before it can move on.

**NewA.h**

```
class NewB;

class NewA
{
   public:

   NewB* MyB;
};

```

**NewB.h**

```
class NewA;

class NewB
{
  public:
  NewA* MyA;
};

```

---

<div class="post-metadata">

**Author:** ![Erdrik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Erdrik](https://forums.unrealengine.com/u/Erdrik)\
**Post date:** [March 18, 2015, 2:01am UTC](https://forums.unrealengine.com/t/how-to-fix-a-syntax-error-on-variable-declaration-of-custom-class/311464/5 "2015-03-18T02:01:50Z")

</div>

“Additionally, something to remember, is ALWAYS use UPROPERTY above UObject member variables or you will have garbage collection issues.”

Wait, there is a difference between:

```
UPROPERTY(VisibleAnywhere, Category=Spells) float fVar;

```

and:

```
UPROPERTY(VisibleAnywhere, Category=Spells)
float fVar;

```

?

That kind makes a mess of my variable declarations…

Or are you just saying to add a UPROPERTY to just class declarations like ADMagicCharacter and AProjectile?  
It would seem rather messy and disorganized to have to tack that onto every float and int…

---

<div class="post-metadata">

**Author:** ![Crzyhomer](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Crzyhomer](https://forums.unrealengine.com/u/Crzyhomer)\
**Post date:** [March 18, 2015, 3:27pm UTC](https://forums.unrealengine.com/t/how-to-fix-a-syntax-error-on-variable-declaration-of-custom-class/311464/6 "2015-03-18T15:27:32Z")

</div>

You should read up on UObjects and the reflection system (UPROPERTY, UFUNCTION, etc).

[UE4 Relection System blog entry](https://www.unrealengine.com/blog/unreal-property-system-reflection)

I wasn’t commenting on your formatting, either of your statements above are fine. What is important is that any UObject member variables of a class must be marked as UPROPERTY otherwise your memory will be freed up by the engine. UObject memory management is done by garbage collection and it only knows the object is in use if it can follow UPROPERTY variables. There are internal lists of objects and if other objects don’t tell that main list they are using something via UPROPERTY the object will be removed out from under you.

There are lots of powerful things that require UPROPERTY. You don’t have to mark every variable as one, but if you want to replicate over the network, save to disk, edit the value in the editor, debug print variable state, etc you must have them marked.

Some other links I’ve shared in the past

[Intro C++ in UE4](https://www.unrealengine.com/blog/ue4-intro-crash-course-programmers)

[Understanding MyClass.generated.h files](https://www.unrealengine.com/blog/unreal-property-system-reflection)

[Programming Docs](https://docs.unrealengine.com/latest/INT/Programming/index.html)

[Objects/Actors/Framework](https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/index.html)

[Programming Quick Start](https://docs.unrealengine.com/latest/INT/Programming/QuickStart/index.html+)

[Replication](https://docs.unrealengine.com/latest/INT/Programming/Gameplay/Framework/Networking/Replication/index.html)
