【问题标题】:Problems with passing a stream as a parameter将流作为参数传递的问题
【发布时间】: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 in setDate`或返回指示设置是否有效的状态。然后就可以在调用代码中做所有的控制台输出了。
  • @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


【解决方案1】:

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;

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

【讨论】:

    猜你喜欢
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2021-10-18
    • 2011-03-31
    • 2019-05-11
    • 2022-11-29
    • 1970-01-01
    相关资源
    最近更新 更多