Syntax error: missing ';' before '*'

i cannot seem to get this one working.
whenever i move the #include “Tank.h” to the Tankplayercontroller.cpp, it gives me this error in unreal (no intellisense issue though):
CompilerResultsLog: Error: C:\Unreal Projects\UnrealTankGame\BattleTank\Source\BattleTank\TankPlayerController.h(23) : error C2143: syntax error: missing ‘;’ before ‘*’
CompilerResultsLog: Error: C:\Unreal Projects\UnrealTankGame\BattleTank\Source\BattleTank\TankPlayerController.h(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
CompilerResultsLog: Error: C:\Unreal Projects\UnrealTankGame\BattleTank\Source\BattleTank\TankPlayerController.h(23) : error C2238: unexpected token(s) preceding ‘;’

there is no such syntax error because everything works fine when the include in the .h file.

same issue if i move the #include “TankPlayerController.h” from tank.h to tank.cpp.
CompilerResultsLog: Error: C:\Unreal Projects\UnrealTankGame\BattleTank\Source\BattleTank\Tank.h(24) : error C2143: syntax error: missing ‘;’ before ‘*’
CompilerResultsLog: Error: C:\Unreal Projects\UnrealTankGame\BattleTank\Source\BattleTank\Tank.h(24) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
CompilerResultsLog: Error: C:\Unreal Projects\UnrealTankGame\BattleTank\Source\BattleTank\Tank.h(24) : error C2238: unexpected token(s) preceding ‘;’

the lines in question are:
ATank * GetControlledTank() const; // in tankplayercontroller.h
UTankAimingComponent * TankAimingComponent = nullptr; // in tank.h

i honestly dont understand how it Should work, as in - how can the compiler know what ATank/UTankAimingComponent is if its not declared anywhere in the file.h, but instead declared in the cpp file that includes this one? is this somehow gets rearranged in the .obj file?

So, I had this exact same issue. And I was losing my mind trying to figure out why. After doing some research and going over the unreal API coding standards I finally figured out the problem.

In the “Tank.h” file (under “protected”) where we declare…
UTankAimingComponent* TankAimingComponent = nullptr;

You’ll want to tell unreal that this is a class.
class UTankAimingComponent* TankAimingComponent = nullptr;

This applies to every other instance where you want to declare a new method based on your custom class, like in TankPlayerController.h

private:
class ATank* GetControlledTank() const;

I hope this helps. It definitely fixed a lot of problems for me.

6 Likes

Thanks @iShred3d for the class hint! (works for me on UE4.25 with IDE Rider 4UE)

Privacy & Terms