Error C2653: is not a class or namespace name (or: non UClass class)

I’m trying to recreate the JSon save game system posted on UDK Gems, so I created a class which will have static methods to save and load a game.

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "SaveGameSystem.generated.h"

/**
 * 
 */
//UCLASS()
//class USaveGameSystem : public UObject
class FSaveGameSystem
{
public:
	//GENERATED_UCLASS_BODY()
		
	static bool SaveGame(const FString& FileName)
	{
		bool bSuccess = false;
		FBufferArchive ToBinary;
		int32 x = 12312;
		float y = 25234.f;
		ToBinary << x;
		ToBinary << y;
		bSuccess = FFileHelper::SaveArrayToFile(ToBinary, FileName);
		ToBinary.FlushCache();
		ToBinary.Empty();

		return bSuccess;
	} 	
};

The above code compiles, but, if I call this from another file, for exemple from the ShooterCharacter:

FSaveGameSystem::SaveGame("Quicksave.save");

I get the errors:

error C2653: 'FSaveGameSystem' : is not a class or namespace name
error C3861: 'SaveGame': identifier not found

I tried adding #include "SaveGameSystem.h" to the top of ShooterCharacter.cpp, didn’t work. I also checked if SaveGameSystem.h is in ShooterGameClasses.h, it is not. I also tried a rebuild / deleting Intermediate/BuildData.

So the question is: how to properly create a non-object class, and make it globally accessible?

Also, I have another, related question: #include "SaveGameSystem.generated.h" is only necessary for Object-extending classes, right? If I remove that line, I get compile errors telling me to put it back.

PS. Sorry, once again I forgot to put it on the right section :frowning:

You need to move the header file out of the “Classes” folder. It can go anywhere else - but Unreal apparently expects all headers to be UCLASS based and, if they aren’t, you get weird errors like this.

For this reason I now have several headers in my “Private” and “Public” directories - which work just as you’d expect. You can also edit your [project].Build.cs file and add lines such as:

    PrivateIncludePaths.Add("MyProjectFolder/Headers");

…and now you can #include headers that you added into that directory.

1 Like

You can also edit your
[project].Build.cs file and add lines
such as:
PrivateIncludePaths.Add(“MyProjectFolder/Headers”);
…and now you can include headers
that you added into that directory.

This is a neat tip!

thanks for sharing!

Rama

Yes, simply moving the file from Classes/ to Public/ solved the problems. I was also able to remove the #include "SaveGameSystem.generated.h" without problems. Thank you both for the replies!

#Non UClass Classes

I managed to put together a non UCLASS() class, which is my VictoryEdMode plugin for the UE4 editor

as follows

#Folder Structure

If you are using private, classes, public as I am

  • the .h for the non uclass has to go into PUBLIC

  • and you must manually add this .h to YourGame.h

  • you need to include the constructor and destructor yourself
    #.h Sample

    #pragma once
      
      #include "UnrealEd.h" 
      #include "Editor.h"
      //#include "BSPOps.h"
      
      class UVictoryEdEngine;
      
      struct FVButton;
      struct FDropToClosestSurfaceData;
      
      class FVictoryEdAlignMode : public FEdMode
      {
      	
      //Statics
      public:
      	static const FColor RV_VRed;
      	static const FColor RV_VBlue;
      	static const FColor RV_VYellow;
      	static const FLinearColor RV_Red;
      	static const FLinearColor RV_Yellow;
      	static const FLinearColor RV_Blue;
      
      //Display Updates
      public:
      	bool DoSingleDisplayUpdate;
      //Drop to Surface
      public:
      	
      	//if multiple selected, the test runs on each and finds the nearest of nearest surfaces
      	void DropSelectedActorsToNearestSurface();
    
      //Constructor/Destructor
      public:
      	FVictoryEdAlignMode(); 
      	~FVictoryEdAlignMode(); 
    

:slight_smile:

#.cpp Sample

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

//Victory Alignment Mode

#include "YourGame.h"  
//YourGame.h needs to include this class's .h
//and this class's .h must go in public directory, or wherever your YourGame.h is


//~~~~~~~~~~~~~~~~

#define CHECK_VSELECTED if(!VictoryEngine) return; if(!VictoryEngine->VSelectedActor) return;

//~~~ Display Choices ~~~
#define VERTEX_DISPLAY_STARS 		0
#define VERTEX_DISPLAY_3DBOX 		1
#define VERTEX_DISPLAY_RECT 			2
#define VERTEX_DISPLAY_DIAMOND3D 	3
#define VERTEX_DISPLAY_SPHERE 		4
#define VERTEX_SELECTED_MULT		1.333
#define VERTEX_SHAPE_MULT			0.8
//~~~ Defines ~~~

//MAX
#define MAX_VERTEX_COUNT_FOR_DRAWING 10000
#define MAX_VERTEX_COUNT_FOR_DRAWING_SPHERES 5000

//Speeds
#define XYZSPEEDMULT 20

//3D
#define DEFAULT_INSTANT_MOVE_DISTANCE 2048
#define CURSOR_DELTA_DISTANCE_CALC 2048
//HUD
#define VICTORY_TITLE_HEIGHT 38
#define VICTORY_TEXT_HEIGHT 24

//Color
#define RED 		0
#define BLUE		1
#define YELLOW 3

//TIME
#define VICTORY_TITLE_VISIBLE_DURATION 2

//Button Vibes
#define BUTTON_VIBE_VICTORYHOTKEYS 		0

//~~~ Const ~~~
const FLinearColor FVictoryEdAlignMode::RV_Red = FLinearColor(1,0,0,1);
const FLinearColor FVictoryEdAlignMode::RV_Yellow = FLinearColor(1,1,0,1);
const FLinearColor FVictoryEdAlignMode::RV_Blue= FLinearColor(0,0,1,1);
const FColor FVictoryEdAlignMode::RV_VRed = FColor(255,0,0,255);
const FColor FVictoryEdAlignMode::RV_VBlue = FColor(0,0,255,255);
const FColor FVictoryEdAlignMode::RV_VYellow = FColor(255,255,0,255);
	
//I used a custom initialize function instead of using a constructor with parameters
FVictoryEdAlignMode::FVictoryEdAlignMode()
{
	
}
FVictoryEdAlignMode::~FVictoryEdAlignMode()
{	
}

#Writing a Static Class

Writing Static Classes in C++