C++ C++;日期秒数
我正在尝试编写一个类,在这个类中,我希望以“03.02.2019”的格式输入一个日期和一个像4这样的整数,我希望代码所做的是作为结果给我“07.02.2019”。我认为这里的问题是,我没有正确地分配指针,因为我可以看到代码正在做一些正确的事情。它确实计算“07.02.2019”,但我无法将结果返回给我;-)。请查找以下代码:C++ C++;日期秒数,c++,date,C++,Date,我正在尝试编写一个类,在这个类中,我希望以“03.02.2019”的格式输入一个日期和一个像4这样的整数,我希望代码所做的是作为结果给我“07.02.2019”。我认为这里的问题是,我没有正确地分配指针,因为我可以看到代码正在做一些正确的事情。它确实计算“07.02.2019”,但我无法将结果返回给我;-)。请查找以下代码: #include <iostream> #include "Calendar.h" #include <ctime> using namespace
#include <iostream>
#include "Calendar.h"
#include <ctime>
using namespace std;
int main()
{
Calendar MyDate("03.02.2019");
Calendar NewDate;
NewDate = MyDate + 4;
cout << endl << NewDate.print() << endl;
cin.get();
getchar();
return 0;
}
#包括
#包括“Calendar.h”
#包括
使用名称空间std;
int main()
{
日历MyDate(“2019年2月3日”);
日历新日期;
NewDate=MyDate+4;
库特
这将从newdate
字符串构造Calendar
对象,该字符串将调用构造函数:
Calendar::Calendar(string thedate)
{
this->set_Date(thedate);
}
设置日期
:
void Calendar::set_Date(string dertag)
{
TheDate = dertag;
TheDay = stoi(dertag.substr(0, 2));
TheMonth = stoi(dertag.substr(3,2));
TheYear = stoi(dertag.substr(6,4));
if (TheDay > 0 && TheDay < 32 && TheMonth>0 && TheMonth < 13 && TheYear>1900 && TheYear < 3000)
isdate = true;
/*if(isdate)
throw exception!*/
set_Seconds();
}
在构造过程中,*newdate2print\u ptr
未分配任何值,字符串为空-在构造函数调用新字符串之前,它是默认初始化的。您需要在任何位置分配它。例如,在设置秒数内:
void Calendar::set_Seconds()
{
//Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde.
YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
DaySeconds = TheDay * 24 * 60 * 60;
TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
*theNewDate2print_ptr = std::to_string(DaySeconds) + "-" + ....;
}
您的类会泄漏内存。它会分配新对象,但不会在任何地方释放它。此外,还将编写复制构造函数以防止内存泄漏。您应该释放分配的内存:
Calendar::~Calendar()
{
delete theNewDate2print_ptr;
}
成员:
vector<string>AllTheDates;
string TheDate;
int TheYear;
int TheMonth;
int TheDay;
unsigned int YearSeconds;
unsigned int MonthSeconds;
unsigned int DaySeconds;
unsigned int TotalSeconds;
string *theNewDate2print_ptr=new string;
//string theNewDate2print;
bool isdate;
string*theNewDate2print\u ptr=new string;
为什么这是一个指针?非常感谢Kamil!这很有帮助!
void Calendar::set_Seconds()
{
//Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde.
YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
DaySeconds = TheDay * 24 * 60 * 60;
TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
}
void Calendar::set_Seconds()
{
//Diese Funktion berechnet die Sekunden für ein bestimmtes Datum das im Format Calender("03.02.2019") übergeben wurde.
YearSeconds = (TheYear - 1900) * 365 * 24 * 60 * 60;
MonthSeconds = TheMonth * 30 * 24 * 60 * 60;
DaySeconds = TheDay * 24 * 60 * 60;
TotalSeconds = YearSeconds + MonthSeconds + DaySeconds;
*theNewDate2print_ptr = std::to_string(DaySeconds) + "-" + ....;
}
Calendar::~Calendar()
{
delete theNewDate2print_ptr;
}
vector<string>AllTheDates;
string TheDate;
int TheYear;
int TheMonth;
int TheDay;
unsigned int YearSeconds;
unsigned int MonthSeconds;
unsigned int DaySeconds;
unsigned int TotalSeconds;
string *theNewDate2print_ptr=new string;
//string theNewDate2print;
bool isdate;
unsigned int TheYear;
unsigned int YearSeconds() {
// TODO: leap years
return TheYear * number_of_seconds_in_a_year;
}
void SetTheYearFromSeconds(unsigned int seconds) {
// TODO: leap years
TheYear = seconds / number_of_seconds_in_a_year;
}
// etc...
unsigned int TotalSeconds() {
return YearSeconds() + .... ;
}