1

I am new to c++. Now I am learning header file.

Sales_data.h

#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <string>
struct Sales_data
{
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};
#endif

test.cpp

#include <iostream>
using namespace std;
#include "Sales_data.h"

int main(){
    Sales_data book = {"zv", 1, 2};
    cout << book.bookNo;
}

If i put those two files in the same folder, it works perfectly. But when i put the sales_data.h in a seperate folder like the following structure:

  • include
    • Sales_data.h
  • src
    • test.cpp

then i got the following error: test.cpp:5:10: fatal error: Sales_data.h: No such file or directory

My c_cpp_properties.json under .vscode looks like this.

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/include"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\msys64\\mingw64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}

Any advice will be appreciated!

Ron
  • 11
  • 2
  • 4
    Either: `#include "..\include\Sales_data.h"` (presuming you are compiling from the src directory) or adjust your build environment's include file search path. – Avi Berger Jul 06 '22 at 03:38
  • 2
    A textbook quality header, by the way. Two thumbs up. None of the usual mistakes that mess up new C++ programmers. – user4581301 Jul 06 '22 at 04:13
  • @AviBerger It works by #include "..\include\Sales_data.h". Could you please elaborate more about the environment solution? Thanks a lot! – Ron Jul 06 '22 at 05:32
  • @0x5453 I think that's not the same question. I used "" to include a custom header file. Maybe it is a includePath error. – Ron Jul 06 '22 at 06:02

1 Answers1

0

If you are following header files are in separate folder please mention the folder name first.

#include "..\include\Sales_data.h"
user4581301
  • 33,082
  • 7
  • 33
  • 54
Wai
  • 54
  • 3
  • It works with ```#include "../include/Sales_data.h``` Is there a way to avoid using the folder name? – Ron Jul 06 '22 at 05:58
  • @Ron Yes. You can specify the location of folders to the compiler. I'm unfamiliar with VSCode, but `"${workspaceFolder}/include"` looks about right. If it doesn't work, I'd search for a question specifically about it and ask if I couldn't find one. Then you use angle brackets instead of quotes: `#include `. The `<>` means the preprocessor will search for the file in the include paths. `""` means the preprocessor will search the folder the file being compiled is in. – user4581301 Jul 06 '22 at 15:26
  • I almost never use `""` because if you change the structure of a complicated program , you'll have to go edit many files. With `<>` incudes you typically have to edit only one (or a single update to an IDE configuration page). – user4581301 Jul 06 '22 at 15:27