Error defining the methods of a class

0

I have this date.h , where I have a class Date . I define my methods and then implement them in a date.cpp but the compiler throws me errors of:

  

error: "method ...." previously defined here. and error: redefinition of "method ....".

I get an error and I do not know why. I do not know what I'm doing wrong. Help.

codes:

date.h:

#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED


using namespace std;

class Date {
private:
    int year;
    int day;
    int month;
    string fullDate;
public:
    int getYear();
    int getDay();
    int getMonth();
    string getFullDate();
    void setYear(const int&);
    void setDay(const int&);
    void setMonth(const int&);
    void _fullDate();
};

and date.cpp:

#include <iostream>
#include <string>
#include <sstream>
#include "date.h"
using namespace std;

int Date::getYear(){
    return year;
    }

int Date::getDay(){
    return day;
    }

int Date::getMonth(){
    return month;
    }

string Date::getFullDate(){
    return fullDate;
    }

void Date::setYear(const int& x){
    year = x;
    }

void Date::setDay(const int& x){
    day = x;
    }

void Date::setMonth(const int& x) {
    month = x;
    }

void Date::_fullDate(){
    stringstream full;
    full << day << "/" << month << "/" << year;
    fullDate = full.str();
}



#endif // DATE_H_INCLUDED
    
asked by Cristian Alvarez 02.09.2017 в 03:49
source

1 answer

3

I have tried the code that you have passed and I have corrected it until the compilation error has been eliminated. It is reduced to:

  
  • Include the string library.
  •   
  • The correct use of the preprocessing technique to not include the same header file several times is to add the #endif to the end of the file of the interface not of the implementation.
  •   

The file date.h

#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
#include <string> // para que pueda emplear los string

using namespace std;

class Date {
private:
    int year;
    int day;
    int month;
    string fullDate;
public:
    int getYear();
    int getDay();
    int getMonth();
    string getFullDate();
    void setYear(const int&);
    void setDay(const int&);
    void setMonth(const int&);
    void _fullDate();
};

#endif // se añade al fichero .h para evitar que un fichero de encabezado se incluya varias veces

The file date.cpp

#include <iostream>
#include <string>
#include <sstream>
#include "date.h"
using namespace std;

int Date::getYear(){
    return year;
}

int Date::getDay(){
    return day;
}

int Date::getMonth(){
    return month;
}

string Date::getFullDate(){
    return fullDate;
}

void Date::setYear(const int& x){
    year = x;
}

void Date::setDay(const int& x){
    day = x;
}

void Date::setMonth(const int& x) {
    month = x;
}

void Date::_fullDate(){
    stringstream full;
    full << day << "/" << month << "/" << year;
    fullDate = full.str();
}
    
answered by 02.09.2017 в 16:25