I 'm learning C++ class.
Here I write a struct call Day. Every function in it are checked careffully
#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;
}
After my check, this should be no problem(maybe).
Then I write another class.
#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
when I compile them with g++ Day.cpp Employee.cpp
then comes to this
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
After reading many answer, It seem to be the problem of linking. But I can't find the redefinition after a long time checking.