3

好的,所以我正在构建一个 Date 类

class Date
{
    private:
    int d;
    int m;
    int y;

 public:
    Date();
    Date(int&, int&, int&);
    Date(const Date&);
    bool isLeap();
    void setDay(int& day)
    {d = day;}
    void setMonth(int& month)
    {m = month;}
    void setYear(int& year)
    {y = year;}
    int getDay() const
    {return d;}
    int getMonth() const
    {return m;}
    int getYear() const
    {return y;}
    void print(ofstream& outfile)
    {
        outfile << "The date in this class is: " <<  << m << "/" << d << "/" << y;
    }

    Date operator= (const Date&);
};

我在 main 中创建了一个 ofstream 并打开了文件。我也确保通过引用传递它!每次我尝试编译时都会收到错误消息:expected primary-expression before '<<' token message... 这是我的主要功能。让我知道我是否需要其他任何东西。

    int main()
{
    int day;
    int month;
    int year;
    Date d1;
    ofstream outfile("Print.txt");
    int maxDays;

    Date d2 = d1;

    cout << "Please enter the 4 digit year: ";
    cin >> year;
    if (year > 0)
    {
        d1.setYear(year);
        cout << "Please enter the 2 digit month: ";
        cin >> month;
        if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
            maxDays = 31;
        else if (month==4||month==6||month==9||month==11)
            maxDays = 30;
        else if (month==2&&d1.isLeap()==false)
            maxDays = 28;
        else if (month==2&&d1.isLeap()==true)
            maxDays = 29;
        if (month < 12)
        {
            d1.setMonth(month);
            cout << "Please enter the 2 digit day: ";
            cin >> day;
            if (day <= maxDays)
                d1.setDay(day);
            else
            {
                cout << "Invalid input, will set to default." << endl;
                d1 = d2;
            }


        }
        else
        {
            cout << "Invalid input, will set to default." << endl;
            d1 = d2;
        }
    }

    else
    {
        cout << "Invalid input, will set to default." << endl;
        d1 = d2;
    }

    d1.print(outfile);

    if(d1.isLeap()==true)
        cout << "This is a leap year\n";
    if(d1.isLeap()==false)
        cout << "This is not a leap year\n";

    cout << "The date is " << d1.getDay() << "/" << d1.getMonth() << "/" << d1.getYear() << endl;

    outfile.close();

    return 0;
}

任何帮助将不胜感激...

4

2 回答 2

5

这一行:

outfile << "The date in this class is: " <<  << m << "/" << d << "/" << y;

有两个<<,他们之间什么都没有。

编辑说明:根据您提到的错误消息,我猜您正在使用 GCC。您可能有兴趣尝试clang,在这种情况下会产生更好的错误消息:

example.cpp:30:54: error: expected expression
    outfile << "The date in this class is: " <<  << m << "/" << d << "/" << y;
                                                     ^
1 error generated.

如果你是一个初学者,它可能会为你节省大量时间,而不是追查可能由轻微语法错误产生的 bizarro GCC 错误消息。

于 2013-01-30T02:26:07.867 回答
1

不是您要求的,但是,如果您将其旋转一点:

    if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
        maxDays = 31;
    else if (month==4||month==6||month==9||month==11)
        maxDays = 30;
    else if (month==2&&d1.isLeap()==false)
        maxDays = 28;
    else if (month==2&&d1.isLeap()==true)
        maxDays = 29;

像这样:

    if (month==2)
    {  
       if (d1.isLeap())
         maxDays = 29;
       else
         maxDays = 28; 
    }
    else if (month==4||month==6||month==9||month==11)
        maxDays = 30;
    else
        maxDays = 31;

在我看来,您的代码更容易阅读。

于 2013-01-30T02:35:05.827 回答