c++ - 错误 : expected primary-expression before '<<' token

标签 c++ file class ofstream

好的,我正在构建一个 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;
}

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

最佳答案

这一行:

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.

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

关于c++ - 错误 : expected primary-expression before '<<' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595666/

相关文章:

c++ - 使用以下 has_member 函数时 SFINAE 无法正常工作的原因是什么?

c - fseek(f,0,2);如果没有 SEEK_CUR/SEEK_SET 或 SEEK_END,这是如何工作的?

python - 为什么从文件数据打印时出现错误,但从命令行打印时却没有

Linux:用一个异常文件删除所有早于某个日期的文件

javascript - 如何在WP中找到特定的类

python - 将类的名称传递给函数,在方法之外分配给类属性

c++ - 计算字节数组中的零 b 位子序列

c++ - 用C++设计一个有助于减少 "repetitive operations on objects"的小对象池

python - Python中如何从当前对象的方法中调用另一个对象的方法

c++ - 容器迭代器的部分特化失败