3

I get an error when creating an object of a class stating that "Error C2011 'dateType': 'class' type redefinition" I've checked my class multiple times and I don't seem to get my hands on the cause of the error

dateType.h

    #include <iostream>;
#include<string>;

using namespace std;

class dateType {
public:
    dateType();
    ~dateType();
    void setDate(string, int, int);

    void printDate()const;



private:
    string  day;
    int month;
    int year;

};

dateType.cpp

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



dateType::dateType()
{
    cout << "please imput day,month,year";
    cin >> day >> month >> year;
}


dateType::~dateType()
{
}

void dateType::setDate(string d, int m, int y) {
    day = d;
    if (m <= 12)month = m;
    else { month = 0; year++; }
    year = y;


}
void dateType::printDate()const{
    cout << "day : \n" << day;
    cout << "month : \n" << month;
    cout << "year : \n" << year;

}

Thank you.

Grabinuo
  • 334
  • 2
  • 11
  • 2
    `#include;` what is that semicolon doing there? You don't need semicolons for preprocessor directives. – Caninonos Mar 03 '18 at 11:48
  • 2
    There is no error in this two files. I integrated them into a clean projectand it worked perfectely. I think you are linking them twice in your project. You should use header guards. See a good explantation on [Wikipedia](https://en.wikipedia.org/wiki/Include_guard). Also you should remove the `;` after your includes + the use of std is not a good style. Try to write std::string. –  Mar 03 '18 at 11:50
  • 4
    Btw, probably unrelated but never ever put `using namespace std;` in a header file (or anything which might be included in another file). That's asking for trouble ([Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). – Caninonos Mar 03 '18 at 11:51
  • @Kali Did you include dateType.cpp in the file with main? – Vlad from Moscow Mar 03 '18 at 11:58
  • 1
    https://en.wikipedia.org/wiki/Include_guard – juanchopanza Mar 03 '18 at 11:59
  • @VladfromMoscow no, I didn't include it in the main, the problem has been solved by adding {#pragma once } – Grabinuo Mar 03 '18 at 12:03
  • **never** put `using namespace std;` in the global namespace of a header file. It can cause many hard to find bugs. Try removing that. – Galik Mar 03 '18 at 12:22

1 Answers1

0

They problem was solved by adding #programa once so the code looks like this:

dateType.h

#pragma once
#include <iostream>
#include<string>



class dateType {
public:
    dateType();
    ~dateType();
    void setDate();

    void printDate()const;



private:
    std::string  day;
    int month;
    int year;

};

dateType.cpp

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



dateType::dateType()
{

}


dateType::~dateType()
{
}

void dateType::setDate()
{
    string d;
    int m;
    int y;
    cout << "please imput day,month,year";
    cin >> d >> m >> y;
    day = d;
    if (m <= 12)month = m;
    else { month = 0; year++; }
    year = y;


}
void dateType::printDate()const{
    cout << "day : "<< day<<endl;
    cout << "month : " << month<<endl;
    cout << "year : " << year<<endl;

}

also using namespace std; was removed. I hope this will help if anyone went through the same error.

Thank you.

Grabinuo
  • 334
  • 2
  • 11
  • Just a note, in your setDate function, the statement year++ does nothing really because you then set year to y. Did you mean y++ maybe? – Zebrafish Mar 03 '18 at 14:01