When I create a new C++ Class in a Subfolder


I am very to C++ and Unreal and I searched for several days for an answer, but couldn’t find any so my apology if there was one somewhere.

Ok, when I create a new class. In my case I want create an
Actor Class
with the name:
Flyer (Flyer.cpp + Flyer.h)

in a subfolder called:

Then the Unreal generates a Flyer.cpp with the following include file:
#include “Dynamics/Flyer.h”
which of course fails to compile, since both files the cpp and the header file were generated in the same path. Of course I can manually fix this problem, but I can’t believe that this is the normal case for everyone else. Have I missconfigured something somewhere that I constantly get the wrong #includes?

What does your general source directory look like?

All my source files are in subfolders and it works just fine. You’d want to look at your project’s build.cs file and make sure that the PrivateIncludePaths reference the right one(s). You’re probably missing the project path or the project’s public or private path.

The includes will work without the build.cs settings since, if you aren’t using subfolders, includes without paths are always allowed to reference files in the same directory as the file doing the include.

1 Like


Unfortunately I don’t know anything about these things. What I did was:

  1. Create New Project: “HelloWorld” and Unreal (4.26.2) created a Project folder for me:

  2. Then in the Unreal Editor I went to:
    \C++ Classes\HelloWorld\

  3. Right clicked and selected: New C++ Class

  4. I selected: Actor and clicked Next

  5. I chose: Flyer as Name
    chose: E:/Unreal Projects/HelloWorld/Source/HelloWorld/Dynamics/ as Path

  6. I hit Create Class

Here I ended up with an error message and some hints that I have to re-compile the project before I would be able to see them in the Unreal Editor.

Back in Visual Studio, I now have the following created files:

when I try to build HelloWorld I end up that Visual Studio can’t find “Dynamics/Flyer.h” which is a result of the bad include command inside of Flyer.cpp (#include “Dynamic/Flyer.h”)

The Idea behind this actor was to work toward an object that would be visible in the world and eventually move it around.

Hope this explain better what I did.

I don’t actually know if you can fix this. Unreal Engine and Visual Studio do not play well together, and it’s really annoying.

I’d recommend using Rider for Unreal. It’s free right now, probably will cost money in the future.
You’ll still encounter this problem every now and then, but Rider will fix it automatically on your next compile.

1 Like

If you’re in Visual Studio, you should be able to find a file named “HelloWorld.Build.cs” either through the Solution Explorer window or however you normally jump to other files. It’s going to be a bunch of C# but don’t worry if you’ve never messed with that language before.

The top of the file will look something like this:

public class HelloWorld : ModuleRules
	public HelloWorld(ReadOnlyTargetRules Target) : base(Target)

Inside that constructor, you’ll want to add (or modify the existing code) so that you have

   new string[] {
      // ... add other private include paths required here ...

within that function.

This should cause Unreal to execute compiles such that your includes can be whatever comes after …\Source\HelloWorld.

Why it didn’t do this by default, I’m not sure. I think it gets sort of confused when making modules that don’t have Public/Private folders. (In fact the PrivateIncludePaths may already include “HelloWorld/Private”, which doesn’t really do you any good).

1 Like

I always get the same issue and I simply delete the name of the folder from the include. I’m not sure why this happens, but don’t let it bother you. Just fix the #include to be:

#include “Flyer.h”

1 Like

I will look into this cs file and I will also try to understand the Private/Public folder thing and for what they’re good for. Thanks a lot for the help!

I had the same issue when starting w Unreal and took me a while to figure out. Some new projects seem to not set up the private and public folders properly for Unreal to use with C++. You want to make sure you are adding your new class to the Public folder for the proper setup and compile… ie:
/C++ Classes/projectname/Public/WhateverFolder/AnotherFolder/

Download the content examples or another project and look at the structure of the folders there to see how it should be set up. I’m not sure why it doesn’t set up properly, but I manually changed mine to the proper structure then re-built the .sln file and things worked fine afterwards.

1 Like

@MrBadDay It’s good to know, but ultimately the public and private folders won’t really come into play until your working with multiple modules or writing you own plugins (not necessarily for sharing but personal organization). My game module doesn’t have either of them for example, but all my plugins do.

@Chip_Burwell That solution works great with the default behavior of includes that I described earlier but does fall apart once you have subfoldered code that you’re trying to access from another subfolder.

1 Like