gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:02:51 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com