![](/img/trans.png)
[英]Linux C++: How do I display my Text application output outside of stdout?
[英]How do I display the output of my C++ class
大家好,我正在嘗試在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
函數似乎沒有實現。
您的頭文件中有它,但dateClass.cpp文件中似乎沒有它。 您需要在dateClass.cpp文件中實現該功能,類似於對setDate
。
您收到錯誤消息是因為它找不到任何實現。
出現錯誤的原因是setDate()
是一個void
函數。 當您嘗試在行中使用setDate()
時:
cout << "The current date is: " << setDate() << endl;
您會收到一個錯誤消息,因為setDate()
需要返回一個std::ostream &
object或其他可轉換的數據。 我建議純粹使用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;
}
好吧,一方面,您正在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;
這會將您的日期輸出到控制台,格式如下:2016/3/12。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.