0

我已经尝试了很多不同的解决方案来解决这个问题,但我似乎无法弄清楚为什么编译器继续在我的头文件中给我这个错误。如果有人可以请给我一些见解,那将非常感激。编辑:抱歉忘记了哪一行给出了错误。它在头文件中的行: Date(string mstr, int dd, int yy);

是的,我知道 this = new Date... 是一个糟糕的解决方案,我只是在解决它;)

标题:

#include <string>

#ifndef DATE_H
#define DATE_H

class Date{
    public:
        Date(int mm, int dd, int yy);
        Date(string mstr, int dd, int yy);
        void print();
        void printFullDate();
        void prompt();

        void setMonth(int);
        void setDay(int);
        void setYear(int);

        int getMonth();
        int getDay();
        int getYear();

        static const int monthsPerYear = 12;
    private:
        int month;
        int day;
        int year;

        int checkDay(int);
};

#endif

如果你需要,这里是实现(它还没有完全完成,我只是想测试我写的一些函数):

#include <iostream>
#include <stdexcept>
#include "Date.h"
using namespace std;

Date::Date(int mm, int dd, int yy){
    setMonth(mm);
    setYear(yy);
    setDay(dd);
}

Date::Date(string mstr, int dd, int yy){
    cout << "It's working";
}

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

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

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

void Date::setDay( int dd ){
    day = checkDay(dd);
}

void Date::setMonth( int mm ){
    if( mm > 0 && mm <= monthsPerYear)
        month = mm;
    else
        throw invalid_argument("month must be 1-12");

}

void Date::setYear( int yy ){
    year = yy;
}

int Date::checkDay( int testDay){
    static const int daysPerMonth[ monthsPerYear + 1 ] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    if( testDay > 0 && testDay <= daysPerMonth[ getMonth() ])
        return testDay;
    if( getMonth() == 2 && testDay == 29 && (getYear() % 400 == 0 || ( getYear() % 4 == 0 && getYear() % 100 != 0 ) ) )
        return testDay;
    throw invalid_argument("Invalid day for current month and year");
}

void Date::print(){

}

void Date::printFullDate(){

}

void Date::prompt(){
    int userChoice = 1;
    int mm, dd, yy;
    string monthStr;

    while(userChoice != 3){
        cout << "Enter 1 for format: MM/DD/YYYY" << endl;
        cout << "Enter 2 for format: Month DD, YYYY" << endl;
        cout << "Enter 3 to exit" << endl;
        cout << "Choice: " << endl;
        cin >> userChoice;
        while(userChoice < 1 || userChoice > 3){
            cout << "Please enter a number 1 - 3 for the formats above." << endl;
            cout << "Choice: " << endl;
            cin >> userChoice;
        }
        if(userChoice != 3){
            switch(userChoice){
                case 1:
                    cout << "Enter Month (1 - 12): ";
                    cin >> mm;
                    setMonth(mm);
                    cout << "Enter Day of Month: ";
                    cin >> dd;
                    setDay(dd);
                    cout << "Enter Year: ";
                    cin >> yy;
                    setYear(yy);
                    break;
                case 2:
                    cout << "Enter Month Name: ";
                    cin >> monthStr;
                    cout << "Enter Day of Month: ";
                    cin >> dd;
                    cout << "Enter Year: ";
                    cin >> yy;
                    this = new Date(monthStr, dd, yy);
                    break;
                default:
                    break;
            }
        }
    }
}
4

2 回答 2

10

问题 #1:string

#include <string>

问题 #2:使用完全限定的std::string而不是 just string,或者在类定义之前放置一个 using 声明:

using std::string;

问题 #3:您不能重新分配this指针:

this = new Date(monthStr, dd, yy); // ERROR!

您正在尝试做的事情可能应该重写为:

*this = Date(monthStr, dd, yy);
于 2013-03-08T15:59:46.177 回答
2

在代码开头使用std::string或声明。using namespace std;

于 2013-03-08T16:01:09.780 回答