Visual Studio – C++ – New class files location

Software:
Microsoft Visual Studio 2022

I’m not sure whether this issue is about different settings between VS 2019 and 2022, or old project source folder structure ported to a new VS project,
But I encountered an annoying problem, of newly generated class files that can’t include or be included by other classes that already exist in the project.
Forgetting that the VS explorer window doesn’t display the actual folder structure, it took me some frustrating investigation to find out, that a new class was simply added to the root folder of the project, rather than the ‘Source’ folder within the project folder, where all the other classes are found.

Fix the problem for an existing class:

Move the new files manually to the correct location, and manually update the .vcxproj file with the new path:
The example in this image shows a new class named ‘baba’, that was moved from the root folder to the ‘Source’ folder, so the path was updated accordingly:

Creating a new class:

In the add new class dialog,
Press the file path button for the .h and .cpp files, and select the wanted folder.
Note:
This may be confusing, as after selecting the path, the dialog doesn’t reflect your selection, but it works.

Fun fact?

I did some searching and..
It seems you can’t set the default location for new classes in a VS C++ project..
If you know otherwise, I would be very grateful if you’ll add a comment about this.

Creating a Visual Studio project from existing code

Software:
Visual Studio 2019

Steps for creating a new Visual Studio project based on existing code files:

  1. Create an empty project folder and name it your intended project name.
  2. Inside the new project folder, create a folder for your source code files.
    * I call is “Source”, not sure if it has to named that way..
  3. Copy your existing code files to the source folder.
  4. Launch Visual Studio, and open it without code:
  5. Select:
    File > New > Project From Existing Code..
    To open the Create New Project From Existing Code Files wizard:
  6. Note:
    The documentation on this operation states that the wizard will copy files by itself, my own experience is that it doesn’t, it just links them to the project, that’s why I copy the source files prior to this step.
    In the Create New Project From Existing Code Files wizard,
    1. Set the path to your project folder.
    2. Specify a project name.
    * I set this to be the same name as the project folder name, not sure if otherwise it will create a sub-folder..
    3. This is set to the same folder as the project folder.
    * It’s possible that I don’t understand this correctly.. but I think theoretically, the intention is that you would add external folders to this list, from which source files would be copied, but like I said, when I tried that the files where not copied to the project.
  7. Set your new projects settings and press Finish to create the new project:

Note:
New code files generated in a ported code project may be stored in a wrong folder by default, see this post for solutions.