将流作为参数传递的问题

时间:2016-02-14 21:40:34

标签: c++ visual-c++ stream parameter-passing ostream

所以我应该测试这个程序进行错误检查,以确保日期有效。我刚刚使用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;
    }

    }

1 个答案:

答案 0 :(得分:0)

streambuf myOutput;是个错误。通常,您不会在自己的代码中声明任何类型streambuf的变量;它们被溪流内部使用。

我不确定你要做什么,但其他一些选择包括:

setDate(month, day, year, std::cout);

std::ostringstream oss;
setDate(month, day, year, oss);
// do something with oss.str()

另一个问题是validDate类有一个类成员:

ostream& myOutput;

您的编译错误是因为您必须为参考成员提供初始化程序。在我看来,这是一个错误;我建议删除该行。