c++ - 当我尝试包含头文件时,为什么会出现错误“没有这样的文件或目录”?
问题描述
我有main.cpp
和add.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);
解决方案
#include <add.h>
导致编译器在系统包含目录中搜索文件add.h
,而不是当前目录。如果要包含add.h
当前目录中的文件,请使用:
#include "add.h"
推荐阅读
- javascript - 设置 div 在某个点开始滚动
- javascript - 如何删除从各个元素动态创建的事件侦听器?
- laravel - 在 laravel 关系中执行查询
- python - 私有名称修改( __A 与 __A__ )
- nginx - Docker-Compose 无法从网络访问我的 http
- php - 如果mysql中没有记录,php显示默认图像
- c++ - 向量和其他容器如何在磁盘上工作?
- typescript - 使用 express-validator 时未键入 Express 请求处理程序
- django - 如果 Django 崩溃,在生产模式下会发生什么?
- python - 如何使用 for 循环创建 numpy 数组