我正在学习C ++课程。
这里我写了一个struct call Day。其中的每个功能都经过仔细检查
#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
Day.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_
Employee.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("编号: %d\n", this->num);
cout << "姓名: " << this->name<<'\n';
if (this->sex)
printf("性别: 男\n");
else
printf("性别: 女\n");
printf("生日: ");
this->birth.print();
}
Employee.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
阅读了很多答案后,似乎是连接的问题。但经过长时间的检查,我找不到重新定义。
答案 0 :(得分:1)
您忘记完成多次包含Employee.h的检查。您的文件以:
开头#ifndef _EMPLOYEE_H_
#include "Day.h"
什么时候应该开始:
#ifndef _EMPLOYEE_H_
#define _EMPLOYEE_H_
#include "Day.h"
答案 1 :(得分:1)
最后,我发现了我的问题。我纠正了我的代码中的错误。我的目录中有一个旧的.gch文件。我删除.gch文件后编译。问题解决了。谢谢大家帮帮我。
答案 2 :(得分:0)
这不是完整的Employee标题,它说它在65行定义而你只粘贴了25行。你可能在Employee标题和employee.cpp中定义它