我在Visual Studio中获得了一个中止对话框。
这是代码:
#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;
}
请注意,我没有使用预编译的标头,因为我已在终端中编译它并尝试在Visual Studio中对其进行调试。在终端中我得到的错误是:
MYPROG.EXE
未找到入口点
程序入口点 _ZNKSt7__cxx1112basic_stringlcSt11char_traitsIcESalcEE7_M_dataEv可以 不能位于动态库链接C:...... \ myprog.exe
我很远不了解这意味着什么。有人能帮助我吗?
我认为这是运行时错误?我搜索过类似的错误,其中大部分与dll有关。是这种情况吗?
编辑:
我用g++ myProg.cpp -o myprog
答案 0 :(得分:0)
string operator +没有任何接受unsigned
右侧的重载。
替换
std::string day_s = (this->day < 10) ? "0" + this->day : std::to_string(this->day);
与
std::string day_s = (this->day < 10) ? "0" + std::to_string(this->day) : std::to_string(this->day);
等等。