好的,所以我正在构建一个 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;
}
任何帮助将不胜感激...