C++:错误:类的重新定义

C++:error: redefinition of Class

本文关键字:新定义 定义 错误 C++      更新时间:2023-10-16

我正在学习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