C++ 头文件C+中出错+;
有人能告诉我这行代码有什么问题吗C++ 头文件C+中出错+;,c++,C++,有人能告诉我这行代码有什么问题吗 #ifndef TIME12_H #define TIME12_H #include <iostream> using namespace std; #include "time24.h" class time12 { private: bool pm; int hours; int minutes; int seconds; public: time12(); time12(bool p, i
#ifndef TIME12_H
#define TIME12_H
#include <iostream>
using namespace std;
#include "time24.h"
class time12
{
private:
bool pm;
int hours;
int minutes;
int seconds;
public:
time12();
time12(bool p, int h, int m, int s);
time12(time24 a);
friend ostream& operator<<(ostream &out, time12 &a);
friend istream& operator>>(istream &in, time12 &a);
int get_hours(){ return hours; };
int get_minutes(){ return minutes; };
int get_seconds(){ return seconds; };
};
#endif // TIME12_H
我正在将Code::Blocks IDE与MinGW一起使用
time24.h
文件是:
#ifndef TIME24_H
#define TIME24_H
#include "time12.h"
#include <iostream>
using namespace std;
class time24
{
private:
int hours;
int minutes;
int seconds;
public:
time24();
time24(int h, int m, int s);
friend ostream& operator<<(ostream &out, time24 &a);
friend istream& operator>>(istream &in, time24 &a);
int get_hours(){ return hours; };
int get_minutes(){ return minutes; };
int get_seconds(){ return seconds; };
};
#endif // TIME24_H
\ifndef时间24小时
#定义时间24小时
#包括“时间12.h”
#包括
使用名称空间std;
上课时间24
{
私人:
整小时;
整数分钟;
整数秒;
公众:
时间24();
时间24(整数小时,整数米,整数秒);
friend ostream&operator(istream&in,time24&a);
int get_hours(){return hours;};
int get_minutes(){return minutes;};
int get_seconds(){return seconds;};
};
#endif//TIME24_H
如果该类错误出现在以下行中:
time12(time24 a);
通常的原因是未定义time24
类型
包含a
标记的仅有三行是:
time12(time24 a);
friend ostream& operator<<(ostream &out, time12 &a);
friend istream& operator>>(istream &in, time12 &a);
然后,当您需要该类时,该类将存在(被声明),但处于未定义状态。您的错误可能是因为您的get_hours()函数实现以及其他函数的末尾有一个额外的分号。你有 int get_hours(){…}
但是你应该
int get_hours(){…}编译器没有指定行号?@hacker804,为什么你的
time24.h
包含time12.h
,而它什么都不使用?我想我写了一个函数,把time12对象作为参数,但我删除了它,忘了删除#include指令。删除它,程序运行正常。Thanks@hacker804,是有道理的,不需要你所拥有的,但我已经添加了答案,以防你需要重新介绍。我还将您的另一个头文件移到了您的问题中,因为最好让所有内容都独立。很高兴我能帮忙。
time12(time24 a);
friend ostream& operator<<(ostream &out, time12 &a);
friend istream& operator>>(istream &in, time12 &a);
class time12; class time24;
#include "time24.h" #include "time12.h"
class time12 { class time24 {
: :
} }