【发布时间】:2016-05-25 17:03:34
【问题描述】:
所以我应该测试这个程序以进行错误检查,以确保日期有效。当我刚刚使用cout 时,一切正常,但我需要将输出或错误消息发送到文件。
我只担心validDate 类,dateType 类工作正常。我尝试将ostream& myOutput 作为void validDate::setDate(int month, int day, int year) 的参数传递,但我必须更改validDate 的参数,这最终对我不起作用。任何想法都会很棒。
#include <fstream>
#include <iostream>
using namespace std;
class dateType {
public:
dateType(int month = 1, int day = 1, int year = 0);
void setDate(int month, int day, int year);
int getDay() const;
int getMonth() const;
int getYear() const;
void printDate(ostream& myOutput) const;
protected:
int dMonth;
int dDay;
int dYear;
};
class validDate:public dateType {
public:
validDate(int month = 1, int day = 1, int year = 0);
void setDate(int month, int day, int year, ostream& myOutput);
ostream& myOutput;
};
int main() {
ofstream myOutput("outfile");
if (myOutput.fail()) {
return -1;
}
validDate badDay;
myOutput << "\n\nvalidDate badDay;\n";
myOutput << "--------------------\n";
badDay.setDate(2, 29, 2015);
myOutput.close();
system("pause");
return 0;
}
dateType::dateType(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
void dateType::setDate(int month, int day, int year)
{
dMonth = month;
dDay = day;
dYear = year;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getYear() const
{
return dYear;
}
void dateType::printDate(ostream& myOutput) const
{
myOutput << dMonth << "-" << dDay << "-" << dYear;
}
validDate::validDate(int month, int day, int year)
{//GETTING ERROR HERE, C2512 no appropriate default constructor available
streambuf myOutput;
setDate(month, day, year, myOutput);
}
void validDate::setDate(int month, int day, int year, ostream& myOutput) {
if (year < 0) {
myOutput << "Error: Year must be greater than 0." << endl;
}
else
switch (month)
{
case 4:
case 6:
case 9:
case 11:
if ((day < 0) || (day > 30)) {
myOutput << "Error: Month must have between 1-30 days." << endl;
}
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if ((day < 0) || (day > 31)) {
myOutput << "Error: Month must have between 1-31 days." << endl;
}
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
myOutput << "Error: Month must have between 1-29 days since it
is a leapyear." << endl;
}
else {
myOutput << "Error: Month must have between 1-28 days." << endl;
}
break;
default:
myOutput << "Error: Invalid Month." << endl;
}
}
【问题讨论】:
-
欢迎堆栈溢出!请添加您的语言 (c++) 作为标签。大多数人只会找自己熟悉的标签,所以懂c++的人找不到你的问题。也请修正你的缩进,因为你的代码很难阅读
-
std::ostream没有默认的constructor。构造时需要streambuf参数。 -
传递
ofstream作为参考参数。由于您在构造函数中为其他参数提供了默认值,因此必须首先使用ofstream参数。没有可用于它的合理默认值。还要注意您正在覆盖setDate成员函数,但没有在dateType中将其声明为虚拟。 -
如果您不希望传入`ostream
. Then consider either throwing an exception insetDate`或返回指示设置是否有效的状态。然后就可以在调用代码中做所有的控制台输出了。 -
@PaulRooney 我将它作为参数传递,但我认为我没有正确使用 streambuf。 validDate::validDate(int month, int day, int year) { streambuf myOutput; setDate(月, 日, 年, myOutput); } void validDate::setDate(int month, int day, int year, ostream& myOutput) {'
标签: c++ visual-c++ stream parameter-passing ostream