错误:类(C++)的重新定义
Error: Redefinition of Class (C++)
我正试图弄清楚为什么会出现以下错误:
错误:重新定义"TimeDuration"
// TimeDuration.cpp
#define HOUR 3600
#define MIN 60
#include <iostream>
#include <string>
#include "TimeDuration.h"
using namespace std;
TimeDuration::TimeDuration() {
seconds = 0;
}
void TimeDuration::setDuration(const int sec) {
seconds = sec;
}
void TimeDuration::display() {
// Some code to display the time
}
错误显示在我的头文件中。
// TimeDuration.h
class TimeDuration {
private:
int seconds;
public:
TimeDuration();
void setDuration(const int sec);
void display();
};
错误可能是因为TimeDuration.h 中没有标头保护
头保护的标准方法是在文件写入的开头:
#ifndef TIME_DURATION_H
#define TIME_DURATION_H
在文件末尾:
#endif
您可以使用
#pragma once
在TimeDuration.h文件中,在起始处
相关文章:
- 定义C++新的环境变量并在 bat 文件中使用它
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 从使用概念定义的函数返回新对象
- 如何在C++中为 if 和 else 语句定义新行为
- 如何从 C++ 中的现有模板函数定义新函数
- 自定义分配器,包括放置新案例
- 正在通过const-ref未定义的行为捕获新构造的对象
- 如何在CPropertySheet中定义新颜色
- 谁定义新操作员
- 未定义的符号'fixed_address_empty_string':带有protobuf的新张量流运算
- 新的C ,未定义的参考
- boost :: fibonacci_heap:带有比较器重新定义圆形定义错误的嵌套定义
- 宏,用于定义带有添加前缀的新宏
- 取消脱壳:使用模板在多行类型定义中添加一个尖括号后的新行
- C 通过Typedef定义新类型
- 通过在此指针上放置新位置重新初始化对象时未定义的行为
- 在 ss.clear() 之后使用 ss.str( " ") 用于新定义的字符串流
- Eclipse CDT将方法定义定义到标头文件
- 是标头文件中使用“新”定义的成员指针泄漏
- LEX 程序中的未定义定义