类类型重定义错误

问题描述 投票:3回答:1

我在创建类的对象时遇到错误,指出“错误C2011'dateType':'类'类型重定义”我已多次检查我的类,我似乎没有弄清楚错误的原因

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;

}

谢谢。

c++ visual-studio class
1个回答
0
投票

他们的问题通过添加#programa once解决,所以代码如下所示:

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;

}

using namespace std;也被删除了。我希望如果有人遇到同样的错误,这将有所帮助。

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.