g++找不到我的头文件
g++ does not find my header file?
我正试图使用g++在树莓pi上用C++编译一个简单的程序。但是我不断地发现头文件不存在。我确信该文件确实存在,并且它也与源文件位于同一文件夹中。
有人知道吗?我已经报道了十几个谷歌搜索的前4页,但运气不好。
当我在正确的文件夹中时,我使用的命令是:
g++ -v -std=c++0x test.cpp timehandler.cpp -o Test
test.cpp:
#include <iostream>
#include "timehandler.h"
int main ()
{
TimeHandler tOne("2015-12-12 20:00");
TimeHandler tTwo("2015-12-12 21:00");
cout << tOne.timeDiff(tTwo) << endl;
return 0;
}
timehandler.cpp:
#include "timehandler.h"
using namespace std;
//Converts a timestring with "YYYY-MM-DD HH:MM:SS" to a time_t
TimeHandler::TimeHandler(std::string timeString)
{
strptime(timeString.c_str(), "%Y-%m-%d %H:%M:%S", &mTimeInfo);
mTime = mktime(timeInfo);
}
int TimeHandler::getTime()
{
return mTime;
}
double TimeHandler::timeDiff(TimeHandler t)
{
return difftime(this->getTime(),t.getTime());
}
timehandler.h:
#ifndef TIMEHANDLER_H
#define TIMEHANDLER_H
#include <string>
#include <time>
class TimeHandler
{
public:
//Constructor
TimeHandler(std::string timeString);
//Public functions
time_t getTime();
double timeDiff(TimeHandler t);
private:
//Private members
struct tm mTimeInfo;
time_t mTime;
};
#endif
试图编译代码时发现有几个错误:
-
返回类型不一致:
在您的cpp文件中,您有:
int TimeHandler::getTime()
但这应该像你的头文件一样:
time_t TimeHandler::getTime()
-
我还必须将
#include <time>
更改为ctime
(或time.h
):#include <ctime>
-
构造函数
int TimeHandler::getTime()
最后一行的参数不正确,应为:mTime = mktime(&mTimeInfo);
-
总的来说,您缺少
cout
和endl
(或using namespace std
)的名称空间std
:std::cout << tOne.timeDiff(tTwo) << std::endl;
相关文章:
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 我的程序没有从文件中读取并输入数据
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- CMake:在其他目录中找不到我的头文件
- 如何在Visual Studio Code中重命名我的a.exe文件?
- 如何格式化我的文本文件以使其不会一遍又一遍地重复同一行?
- 查找定义我的 C/C++ 函数/宏的文件比'grep'更简单的方法
- 我的文件无法正常工作,无法从C++文件中读取上下文
- 我的 SDL2 程序需要哪些二进制文件,以便它在另一台未安装 SDL2 的计算机中工作
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 我的头文件 DNode 中的我的数据类型未被识别为数据类型
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 如何在我的 Visual C++ 项目中使用 .a 文件
- 为什么我不能从同一目录中的另一个文件中 #include 我的类?
- 如何将我的程序添加到文件和文件夹的macOS右键单击菜单?
- 如何自动获取我的项目的路径并删除一些文件
- :-1: 错误: LNK1104: 无法打开文件"我的项目.obj"
- C++创建进程 - 系统错误#2找不到文件 - 我的文件路径有什么问题?