程序在我的 C++ 工作区中找不到头文件

Program Not Finding the Header File in my C++ Workspace

我是 C++ 编程的新手,一直在关注 C++ Primer 第 5 版这本书。有一个练习需要一个作为头文件提供的对象 'Sales_item'。 link 已过期,所以我从 GitHub 获取代码并自己将其保存为头文件 (.h)。我正在使用 Code::Blocks IDE,并将 Sales_item.h 文件保存在与我的项目文件相同的目录中。当我重新打开项目时,CodeBlocks 自动在工作区中创建了一个 'header' 文件夹,其中包含 Sales_item 文件。我将它添加到我当前项目的顶部并编译,只是为了检查它是否有效:

#include <Sales_item.h>

但是它会返回一个错误并且无法编译。

程序的主体看起来并不重要;它并没有真正做任何事情,只是为了测试,如果我从工作区中删除头文件,它可以编译和运行。我尝试在 Uni 的不同(希望更好)连接上从 GitHub 重新下载代码并重新保存文件,并将头文件移动到工作区 bin 和 obj 文件夹中的不同位置。 错误消息可能暗示我没有使用 C++11?鉴于这是 2018 年的视频中推荐的 IDE,而且书中暗示代码是 C++11,我的印象是。我不太确定如何检查。 我确定这是一个简单的问题,我只是不知道如何解决。编译时的错误消息包括 '#include ,还指向 Sales_item.h 文件本身,包括 3 个错误和 3 个警告(包括在下面)。

#include <iostream>
#include <Sales_item.h>

using namespace std;

int main()
{
    int num1 = 2;
    int num2 = 3;

    std::cout << num1 + num2 << std::endl;

    return 0;

}

||=== Build: Debug in C++PrimerWork (compiler: GNU GCC Compiler) ===|
Documents\C++ Projects\C++Primer\C++PrimerWork\main.cpp|2|fatal error: Sales_item.h: No such file or directory|

Documents\C++ Projects\C++Primer\C++PrimerWork\Sales_item.h|56|warning: defaulted and deleted functions only available with -std=c++11 or -std=gnu++11|

Documents\C++ Projects\C++Primer\C++PrimerWork\Sales_item.h|70|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11|

Documents\C++ Projects\C++Primer\C++PrimerWork\Sales_item.h|71|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11|

Documents\C++ Projects\C++Primer\C++PrimerWork\Sales_item.h|45|error: constructor required before non-static data member for 'Sales_item::units_sold' has been parsed|

Documents\C++ Projects\C++Primer\C++PrimerWork\Sales_item.h|45|error: constructor required before non-static data member for 'Sales_item::revenue' has been parsed|
||=== Build failed: 3 error(s), 3 warning(s) (0 minute(s), 0 second(s)) ===|

第一个错误指出没有 Sales_item.h 这样的文件,尽管它已自动添加到工作区并在其自己的选项卡中打开。进一步的错误也指向文件本身内部(特别是警告)。 底部的两个错误让我认为原始下载在某种程度上已损坏。结合警告,我还想知道编译器是否需要不同版本的 C++,或者我是否做错了什么,但我不知道。

当您使用 < > 包含头文件时,这意味着该文件与其他标准库文件位于同一文件夹中

#include <Sales_item.h>  
//it will look in the same folder that contains file like studio.h,string.h
   #include "Sales_item.h"   
//it will look in folder where the main cpp is stored so try "Sales_item.h"
//or add the path of Sales_item.h in your program properties
  • 寻找 Sales_item.cpp 肯定有一个,因为通常头文件确实有一个同名的 cpp 文件,其中定义了函数

包含文件的两种变体的不同之处在于编译器将在何处搜索要包含的 header 文件。

实际上,要包含的路径在两种变体中都是 implementation defined。但标准也规定:

A preprocessing directive of the form #include "q-char-sequence" new-line [...] if the search fails, the directive is reprocessed as if it read #include <h-char-sequence> new-line [...].

由此可见,您可以将 任何 header 包含在 "" 中,但不一定全部包含在 <> 中。

通常,'implementation defined'搜索路径包含标准库header的路径(例如cstdintvector, ...) 以及您显式提供给编译器的那些(通常通过 -I 命令行选项——但这取决于编译器),而 "" 通常添加要编译的文件所在的目录in(即相对于当前编译文件的路径)。

这样,您可以包含如下路径:

#include "../include/myLibrary/SomeHeader.h"

如果将 header 和源文件放在不同的目录中,可能会发生这种情况。

很常见的约定(即标准未强制要求)包括标准 header 和您 link 反对的第三方库通过 <> 和您自己的项目通过 "".

使用#incude "Sales_item.h"

实际上,区别在于预处理器搜索包含文件的位置。

对于 #include <filename>,预处理器以依赖于实现的方式进行搜索,通常在 compiler/IDE 的搜索目录 pre-designated 中进行搜索。该方法一般用于包含标准库头文件。

对于#include "filename",预处理器首先在与包含指令的文件相同的目录中搜索,然后按照#include 形式使用的搜索路径进行搜索。此方法通常用于包含 programmer-defined 个头文件。