【问题标题】:How do I display the output of my C++ class如何显示我的 C++ 类的输出
【发布时间】:2016-04-11 01:13:21
【问题描述】:

大家好,我正在尝试在 C++ 中创建一个日期类,它显示默认日期、显示短设置日期、显示长设置日期、仅显示设置年份。但是,我尝试在输入数据时显示类的内容,但出现错误“使用未声明的标识符'setDate'

头文件代码如下:

#ifndef dateClass_h
#define dateClass_h

using namespace std;

class DateObj

{

public:
    int setDate(int month, int day, int year);
    void shortDate(int month, int day, int year);
    void longDate(string month, int day, int year);
    //return the year
    int getYear(int year){
        return year;
    }
private:
    int month;
    int day;
    int year;

};
#endif /* dateClass_h */

这是我的实现文件代码:

#include <stdio.h>
#include <iostream>
#include <string>
#include "dateClass.h"

using namespace std;



//setDate Method
int DateObj::setDate(int month, int day, int year){

    //setMonth = month;
    //day = setDay;
    //year = setYear;

    //check to make sure month is between 1 - 12.
    if(month < 1 || month > 12){
        cout << "This is an invalid Month. Please enter a month that is between 1 and 12: " << endl;
    }
    //check to make sure day is between 1 -31.
    if(day < 1 || day > 31){
        cout << "This is an invalid Day. Please enter a day that is between 1 and 31: " << endl;
    }
    //check to make sure year is greater than zero.
    if(year < 0){
        cout << "This is an invalid Year. Please enter a year that is greater than Zero" << endl;
    }

    return setDate(month,day,year);

}

这是我的主程序代码:

#include <iostream>
#include <string>
#include "dateClass.h"

using namespace std;

int main() {

    DateObj myDate;


    myDate.setDate(12, 25, 2016); //set the date to Dec 25, 2016.
    cout << "The current date is: " << setDate() << endl;


    return 0;
}

我只想知道为什么我会收到这个错误,以及我需要在我的课程或主程序中修复什么。

编辑 我得到了要编译的程序,但是在返回 setDate(month, day, year) 断点处出现以下错误“EXC_BAD_ACCESS (code=2, address=0x7fff5f3fffe8)。

#include <iostream>
#include <string>
#include "dateClass.h"

using namespace std;

int main() {

    DateObj myDate;


    myDate.setDate(12,25,2016); //set the date to Dec 25, 2016.
    cout << "The current date is: " << myDate.setDate(12, 25, 2016) << endl;


   return 0;
}

【问题讨论】:

  • 你还没有向我们展示shortDate()函数的实现。
  • 嘿抱歉我的意思是 setDate() 函数。

标签: c++


【解决方案1】:

您的 shortDate 函数似乎没有实现。

您的头文件中有它,但您的 dateClass.cpp 文件中似乎没有它。您需要在 dateClass.cpp 文件中实现它,类似于您对 setDate 所做的那样。

您收到错误是因为它找不到任何实现。

【讨论】:

  • 您好,我修复了我要为 setDate() 函数说的类型。
【解决方案2】:

您收到错误的原因是因为setDate() 是void 函数。当您尝试在该行中使用setDate() 时:

cout << "The current date is: " << setDate() << endl;

因为setDate() 需要返回std::ostream &amp; 对象或其他一些可转换的数据,您会收到错误消息。我建议纯粹使用setDate(int month, int day, int year) 来设置类的数据成员并创建一个单独的函数来实际打印出值。

作为替代方案,您可以重载 setDate() 以使其返回可接受的数据类型,如下所示:

#include <iomanip>
//the parameter 'right' is required for chaining together different << when forming output
std::ostream & DateObj::setDate(std::ostream & right) {
    //displays the date in mm/dd/yyyy format
    //setfill defines a character to fill empty spaces created by setw
    right << std::setfill('0') << std::setw(2) << month
        << '/' << std::setfill('0') << std::setw(2) << day
        << '/' << std::setfill('0') << std::setw(4) << year;

    return right;
}

【讨论】:

    【解决方案3】:

    好吧,一方面,您在 setDate() 中调用 setDate() 的方式是它会无限地调用自己。您将调用 setDate(),然后调用 setDate(),然后调用 setDate(),依此类推。我也是个菜鸟,但我很确定你得到的是堆栈溢出错误(嘿,标题掉了!:D)。当您分配太多变量并用完内存时,会发生堆栈溢出。因此,因为您无限调用 setDate(),所以您也在其中创建无限版本的变量,并填满您的所有内存。

    另一方面,设置日期目前实际上并没有做任何事情。它会检查您是否有有效的输入,然后再次调用自己。从名称来看,我认为它是为了填充您的成员变量?如果这是它应该做的,它应该看起来更像这样:

    int DateObj::setDate(int month, int day, int year){
    
    //check to make sure month is between 1 - 12.
    if(month < 1 || month > 12){
        cout << "This is an invalid Month. Please enter a month that is between 1 and 12: " << endl;
    }
    //check to make sure day is between 1 -31.
    if(day < 1 || day > 31){
        cout << "This is an invalid Day. Please enter a day that is between 1 and 31: " << endl;
    }
    //check to make sure year is greater than zero.
    if(year < 0){
        cout << "This is an invalid Year. Please enter a year that is greater than Zero" << endl;
    }
    //the part that actually sets the variables. Aka, the important bit.
    month_ = month;
    day_ = day;
    year_ = year
    

    }

    月、日和年后面的下划线只是样式,我喜欢将它们用于成员数据,这样我就可以在我的函数中再次将这些名称用作局部变量,如果你注意到的话,这在这里很有用。将数据放入变量后,显示所有内容应该很容易。只需执行以下操作:

    cout << month_ << "/" << day_ << "/" << year_ << endl;
    

    这会将您的日期以如下格式输出到控制台:3/12/2016。

    【讨论】:

    • 我试图计算函数 setDate() 如果我在函数中设置日期,它应该显示 setDate() 但就像我说的我不会运行。
    猜你喜欢
    • 2016-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多