Problem whit Visual Studio / C++

FyMa2618 61 Reputation points
2021-05-27T13:11:10.043+00:00

Good day,
i am new in Visual Studio, i make a C++ project.
On my snl. file i had activated all files usw., but when i try to include a headerdata its dont work(Only when its in a other Folder.) Anyone know what i can do?
Thanks in Advance.

Developer technologies C++
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2021-05-27T13:42:47.413+00:00

    Firstly bear in mind that the Solution Explorer UI for a C++ project does not necessarily indicate the actual folder structure on disk. C++ only cares about the folder structure on disk, not Solution Explorer. In a default C++ project it groups .cpp files under Source Files and .h/.hpp files under Header Files but that has nothing to do with the underlying disk structure.

    When you use a #include you must use the disk structure. So imagine you have this structure on disk (not Solution Explorer as it doesn't matter).

    \MyProject
    MyProject.cxproj
    \Utility
    Utility.h
    \Window
    Window.h
    Window.cpp
    MyCode.cpp

    To reference the utility.h file in MyCode.cpp you would simply include the relative path from the source file #include ".\Utility\Utility.h"

    If you have a heavily nested project structure then note that it gets complicated fast as you have to walk back to the shared root and then down. For example to reference utility.h in Window.cpp you'd need to walk back one directory and then down #include "..\Utility\Utility.h".

    For a really complex project this can get unmanageable so an alternative approach is to modify the project's properties and add the common include paths that you might use as part of the #include search paths. You can then go back to the standard #include "Utility.h" syntax and let the compiler find the files.

    Finally note that the Show All Files features in Solution Explorer is useful for quickly adding a bunch of files that aren't in the project to the project as far as the compiler is concerned. In general you shouldn't leave it on however as this can be confusing. Also note that really only .cpp files matter for the compiler. Header files can be added to the project to make them easier to see and manage but the compiler doesn't care about them unless a source file somewhere has a #include for them.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.