-3

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.

yan liang
  • 65
  • 5
  • 2
    Your header guards in `Employee.h` are not correct. I suggest you remove the header guards and replace them with `#pragma once` – Cory Kramer Apr 12 '18 at 15:45
  • Unrelated to your problem, but not that symbols starting with an underscore and followed by an upper-case letter (like e.g. `_Day_h_`) is reserved in all scopes. See e.g. [this question and its answer](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) for details. – Some programmer dude Apr 12 '18 at 15:47
  • I replace the `#ifndef ..... #define .... #endif` and replace them with `#pragma once` in Employee.h. Problem still exist. And I try to replace the `#ifndef _Day_h_.....` with `#pragma once` or `#ifndef _DAY_H_`. Then the struct in Day.h also redefinition when compiling.I don't sure if this is actually what you mean. Sorry, C is my first programing language. I still have a lot unknown thing to learn – yan liang Apr 12 '18 at 16:33

3 Answers3

1

You forgot to complete the check for multiple inclusion of Employee.h. You file starts with:

#ifndef _EMPLOYEE_H_
#include "Day.h"

When it should really start with:

#ifndef _EMPLOYEE_H_
#define _EMPLOYEE_H_
#include "Day.h"
tdk001
  • 1,014
  • 1
  • 9
  • 16
1

Finally, I find my problem. After I corrected error in my code. I have a old .gch file in my directory. I compile after deleting the .gch file. Problem sloved. Thanks for everyone helped me.

yan liang
  • 65
  • 5
0

That is not full Employee header it say its defined on 65 line and you pasted only 25 lines.You have it defined probably in Employee header and employee.cpp

MarkoShiva
  • 83
  • 1
  • 2
  • 10
  • This is where things get weird. My `Employee.h` has 25 lines. No more, I promise. And the compiler really saying it has 65 lines. I still can't not find redefinition after checking again and again. – yan liang Apr 12 '18 at 16:44
  • second line in your employee header should have ```#define _EMPLOYEE_H_``` – MarkoShiva Apr 12 '18 at 18:04
  • Also why defining day as struct and then use in new class? Isn't more normal to define Day as class and then use it when creating the employee class? – MarkoShiva Apr 12 '18 at 18:07
  • I have that line of code when I compile it. But I miss it when I paste my code here. It is for practices, I think it would be better to have the ability to write both class and struct. I don't think there's any syntax error it my struct. I will change it to class later when I have assess to a computer and see whether problem can be solved. – yan liang Apr 13 '18 at 00:07