C++:错误:类的重新定义
C++:error: redefinition of Class
我正在学习C++课。
在这里,我写了一个结构调用日。其中的每一个功能都经过仔细检查
#ifndef _Day_h
#define _Day_h_
typedef struct Day {
int year;
int month;
int date;
void print()const;
Day (int y,int m,int d);
Day ();
Day(const Day& day);
void setYear(int y);
void setMonth(int m);
void setDate(int d);
int getYear()const;
int getMonth()const;
int getDate()const;
} Day;
#endif
日.h
#include<cstdio>
#include"Day.h"
using namespace std;
Day::Day(int y,int m,int d){
year = y;
month = m;
date = d;
void print();
}
Day::Day(const Day& day){
this->year=day.year;
this->month=day.month;
this->date=day.date;
}
void Day::print()const{
printf("%4d-%2d-%2d",year,month,date);
}
void Day::setYear(int y){
this->year = y;
}
void Day::setMonth(int m){
this->month = m;
}void Day::setDate(int d){
this->date = d;
}
int Day::getYear()const{
return this->year;
}
int Day::getMonth()const{
return this->month;
}
int Day::getDate()const{
return this->date;
}
经过我的检查,这应该没有问题(也许(。
然后我写另一个类。
#ifndef _EMPLOYEE_H_
#include "Day.h"
#include <cstdio>
#include <iostream>
#include <string>
#include <typeinfo>
#define check_int(x) (typeid(x) != typeid(int) || ((x) <= 0))
#define check_str(s) (typeid((s)) != (typeid(std::string)))
#define NOH 10000
class Employee {
private:
int num;
std::string name;
bool sex; // true mean male false mean female
Day birth;
std::string job;
public:
Employee();
Employee(int, std::string, bool, Day, std::string);
void print()const;
~Employee();
};
#endif // !_EMPLOYEE_H_
员工.h
#include"Employee.h"
#include "Day.h"
using namespace std;
Employee::Employee(int number, string nam, bool s, Day day1, string j): num(number), name(nam), sex(s), birth(day1), job(j) {}
void Employee::print() const {
printf("编号: %dn", this->num);
cout << "姓名: " << this->name<<'n';
if (this->sex)
printf("性别: 男n");
else
printf("性别: 女n");
printf("生日: ");
this->birth.print();
}
员工.cpp
当我用g++ Day.cpp Employee.cpp
编译它们时然后来到这个
Employee.cpp:4:1: error: redefinition of ‘Employee::Employee(int, std::__cxx11::string, bool, Day, std::__cxx11::string)’
Employee::Employee(int number, string nam, bool s, Day day1, string j): num(number), name(nam), sex(s), birth(day1), job(j) {}
^
Employee.h:42:1: note: ‘Employee::Employee(int, std::__cxx11::string, bool, Day, std::__cxx11::string)’ previously defined here
Employee.cpp:6:6: error: redefinition of ‘void Employee::print() const’
void Employee::print() const {
^
Employee.h:65:6: note: ‘void Employee::print() const’ previously defined here
看了很多答案,好像是链接的问题。但是经过长时间的检查,我找不到重新定义。
您忘记完成对 Employee.h 的多重包含的检查。您的文件开头为:
#ifndef _EMPLOYEE_H_
#include "Day.h"
什么时候它真正应该开始:
#ifndef _EMPLOYEE_H_
#define _EMPLOYEE_H_
#include "Day.h"
最后,我找到了我的问题。在我纠正代码中的错误之后。我的目录中有一个旧的 .gch 文件。我在删除 .gch 文件后编译。问题很糟糕。谢谢大家帮助我。
这不是完整的员工标题,它说它在 65 行上定义,而您只粘贴了 25 行。您可能在员工标题和员工中定义了它.cpp
相关文章:
- 定义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 程序中的未定义定义