C++ C++;程序入口点错误
我在VisualStudio中收到一个中止对话框。 代码如下:C++ C++;程序入口点错误,c++,visual-studio,dll,runtime-error,C++,Visual Studio,Dll,Runtime Error,我在VisualStudio中收到一个中止对话框。 代码如下: #include <iostream> #include <string> class DateTime { public: DateTime(unsigned year, unsigned month, unsigned date, unsigned hour, unsigned minute, unsigned second); DateTime(unsigned year, unsi
#include <iostream>
#include <string>
class DateTime
{
public:
DateTime(unsigned year, unsigned month, unsigned date, unsigned hour, unsigned minute, unsigned second);
DateTime(unsigned year, unsigned month, unsigned date);
~DateTime();
std::string get_string();
private:
unsigned year;
unsigned month;
unsigned day;
unsigned hour;
unsigned minute;
unsigned second;
};
std::string DateTime::get_string()
{
// dd/MM/yyyy hh:mm:ss
std::string day_s = (this->day < 10) ? "0" + this->day : std::to_string(this->day);
std::string month_s = (this->month < 10) ? "0" + this->month : std::to_string(this->month);
std::string year_s = std::to_string(this->year);
std::string hour_s = (this->hour < 10) ? "0" + this->hour : std::to_string(this->hour);
std::string minute_s = (this->minute < 10) ? "0" + this->minute : std::to_string(this->minute);
std::string second_s = (this->second < 10) ? "0" + this->second : std::to_string(this->second);
return day_s + "/" + month_s + "/" + year_s + " " + hour_s + ":" + minute_s + ":" + second_s;
}
DateTime::DateTime(unsigned year, unsigned month, unsigned date)
{
if (month > 12 || day > 31)
throw std::out_of_range("DateTime out of valid range");
unsigned max_d = 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
max_d = 30;
else if (month == 2) {
max_d = 28;
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
max_d = 29;
}
if (day > max_d)
throw std::out_of_range("DateTime out of valid range");
this->year = year;
this->month = month;
this->day = day;
this->hour = 0;
this->minute = 0;
this->second = 0;
}
DateTime::DateTime(unsigned year, unsigned month, unsigned date, unsigned hour, unsigned minute, unsigned second)
{
if (month > 12 || day > 31 || hour > 24 || minute > 60 || second > 60)
throw std::out_of_range("DateTime out of valid range");
unsigned max_d = 31;
if (month == 4 || month == 6 || month == 9 || month == 11)
max_d = 30;
else if (month == 2) {
max_d = 28;
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
max_d = 29;
}
if (day > max_d)
throw std::out_of_range("DateTime out of valid range");
this->year = year;
this->month = month;
this->day = day;
this->hour = hour;
this->minute = minute;
this->second = second;
}
DateTime::~DateTime()
{
}
int main(int argc, char const *argv[])
{
DateTime d1(2018, 5, 14);
std::cout << d1.get_string();
return 0;
}
#包括
#包括
类日期时间
{
公众:
日期时间(未签名的年份、未签名的月份、未签名的日期、未签名的小时、未签名的分钟、未签名的秒);
日期时间(未签名的年份、未签名的月份、未签名的日期);
~DateTime();
std::string get_string();
私人:
未签字年份;
未签字月份;
未签名日;
未签名小时;
未签名分钟;
无符号秒;
};
std::string DateTime::get_string()
{
//年月日hh:MM:ss
std::string day_s=(本->日<10)?“0”+本->日:std::to_string(本->日);
std::string month_s=(本->月<10)?“0”+本->月:std::to_string(本->月);
std::string year\u s=std::to\u string(本->年);
std::string hour\u s=(本->小时<10)?“0”+本->小时:std::to\u string(本->小时);
std::string minute\u s=(this->minute<10)?“0”+this->minute:std::to\u string(this->minute);
std::string second_s=(此->秒<10)?“0”+此->秒:std::to_string(此->秒);
返回日+“/”+月+“/”+年+“+小时+”:“+分钟+”:“+秒;
}
DateTime::DateTime(未签名的年份、未签名的月份、未签名的日期)
{
如果(月>12日>31日)
抛出标准::超出范围(“日期时间超出有效范围”);
无符号最大值d=31;
如果(月==4 | |月==6 | |月==9 | |月==11)
最大直径=30;
否则,如果(月==2){
最大直径=28;
如果((年份%400==0)| |((年份%4==0)和&(年份%100!=0)))
最大d=29;
}
如果(天>最大值)
抛出标准::超出范围(“日期时间超出有效范围”);
本->年=年;
本->月=月;
这->天=天;
这个->小时=0;
本->分钟=0;
这个->秒=0;
}
DateTime::DateTime(未签名的年份、未签名的月份、未签名的日期、未签名的小时、未签名的分钟、未签名的秒)
{
如果(月>日>小时>24分钟>60秒>60秒)
抛出标准::超出范围(“日期时间超出有效范围”);
无符号最大值d=31;
如果(月==4 | |月==6 | |月==9 | |月==11)
最大直径=30;
否则,如果(月==2){
最大直径=28;
如果((年份%400==0)| |((年份%4==0)和&(年份%100!=0)))
最大d=29;
}
如果(天>最大值)
抛出标准::超出范围(“日期时间超出有效范围”);
本->年=年;
本->月=月;
这->天=天;
这个->小时=小时;
这->分钟=分钟;
这->秒=秒;
}
DateTime::~DateTime()
{
}
int main(int argc,char const*argv[]
{
日期时间d1(2018年5月14日);
std::coutstring运算符+没有任何重载可以接受右侧的无符号
替换
std::string day_s = (this->day < 10) ? "0" + this->day : std::to_string(this->day);
std::string day_s=(本->日<10)?“0”+本->日:std::to_string(本->日);
与
std::string day_s=(本->日<10)?“0”+std::to_string(本->日):std::to_string(本->日);
诸如此类。您的构造函数接受一个您不使用的date
参数,但您将其与day
进行比较,这是您的成员变量当您点击“重试”按钮放入调试器时,您发现了什么?与您的问题无关,但是:而不是声明~DateTime();
对于空的用户定义实现DateTime::~DateTime(){}
,保留析构函数,方法是将其完全省略或声明为~DateTime()=default;
。谢谢@CoryKramer!你的眼睛很锐利!但我仍然得到了错误。@Mark Ransom它在构造函数的第行未签名的max_d=31;
触发了一个断点,有3个参数。也许我必须处理它上面的异常?
std::string day_s = (this->day < 10) ? "0" + std::to_string(this->day) : std::to_string(this->day);