【问题标题】:error: expected primary-expression before '<<' token错误:“<<”标记之前的预期主表达式
【发布时间】:2013-01-13 18:38:24
【问题描述】:

好的,我正在构建一个 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 并打开了文件。我也确保通过引用传递它!每次我尝试编译时都会收到错误消息:在 '

    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;
}

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

【问题讨论】:

  • 你有&lt;&lt; &lt;&lt;,中间没有任何东西。
  • 一个错误是“outfile
  • 这个问题是一个很好的例子,说明为什么你应该尝试提出SSCCE。如果你这样做了,你已经自己解决了这个问题!

标签: c++ file class ofstream


【解决方案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;

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

【讨论】:

  • 非常感谢。我是个彻头彻尾的白痴。我不敢相信我错过了!
  • 我在不同的时间用不同的语言写了几次“一个月有多少天”。有 7 个月和 31 天这一事实使其成为“其他一切”的最佳候选者。 [虽然以运行时的开销稍多为代价——但如果你计算时钟周期,除了闰年的 2 月之外,你无论如何都会使用一个表格。
【解决方案2】:

这一行:

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

有两个&lt;&lt;,它们之间没有任何内容。

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

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

如果您是初学者,则可能会节省大量时间来查找可能由轻微语法错误生成的 bizarro GCC 错误消息。

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多