首页 > 解决方案 > 当我尝试包含头文件时,为什么会出现错误“没有这样的文件或目录”?

问题描述

我有main.cppadd.cpp add.h文件

当我尝试包含时add.h,出现以下编译错误

main.cpp:2:10: fatal error: add.h: No such file or directory
    2 | #include <add.h>
      |          ^~~~~~~
compilation terminated.

这是代码:

main.cpp

#include <iostream>
#include <add.h>
using namespace std;

int main()
{
    cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
    return 0;
} 

add.cpp

int add(int x, int y)
{
    return x + y;
};

add.h

int add(int x, int y);

标签: c++

解决方案


#include <add.h>导致编译器在系统包含目录中搜索文件add.h,而不是当前目录。如果要包含add.h当前目录中的文件,请使用:

#include "add.h"

推荐阅读