获取错误:在抛出'std :: logic_error'的实例后调用terminate():basic_string :: _ M_construct null无效。不知道该怎么做

时间:2017-11-24 19:30:29

标签: c++ class

我正在制作一个适用于日期的“日期”课程,它给了我这个错误:

terminate called after throwing an instance of 'std::logic_error'   
what():  basic_string::_M_construct null not valid

我尝试在网上寻找解决方案,但我找不到任何可能发生这种情况的字符串。

这是我的代码:

测试文件:

#include <iostream>
#include "Date.h"
using namespace std;

int main()
{
    Date d1(2,3,2004), d2(3,5,2005);

    cout << "Date 1 is: ";
    d1.printDate();

    cout << "Date 2 is: ";
    d2.printDate();

    cout << "The month name for Date 1 is: " << d1.getMonthName() << endl;
    cout << "The month name for Date 2 is: " << d2.getMonthName() << endl;

    cout << "The month name when Date 1's month is 7 is: " << d1.getMonthName(7) << endl;

    cout << d1.getDaysBetween(d2);
    return 0;
}

标题文件:

/* date.h */
#ifndef DATE_H_
#define DATE_H_

#include <array>
#include <string>
using namespace std;

class Date 
{
public:
    Date(int=1, int=1, int=2000); // sets day, month, year
    void printDate() const; // print date to the screen
    void setDate(int=1, int=1, int=2000);
    string getMonthName(int);
    string getMonthName();

    int getDaysBetween(Date);

private:
    const array<string, 12> monthNames={"January","February","March","April","May","June","July","August","September","October","November","December"};
    const array<int, 12> daysInMonth={31,28,31,30,31,30,31,31,30,31,30,31}; 

    int day;
    int month;
    int year;

    bool isYearDiff(Date);
    bool isMonthDiff(Date);

    int getYearDiff(Date);
    int getMonthDiff(Date);
    int getYearsDaysBetween(Date);
    int getMonthsDaysBetween(Date);

    bool isDatesCorrect(int, int, int);

    bool isMonthCorrect(int); 
    bool isDayCorrect(int, int);
    bool isYearCorrect(int);
};

#endif /* DATE_H_ */

班级:

/* date.cpp */
#include <iostream>
#include <cmath>
#include "Date.h"
using namespace std;

// Constructor
Date::Date (int m, int d, int y)
{
    setDate(m,d,y);
}

//Function to print date
void Date::printDate() const
{
    cout << month << "/" << day << "/" << year << endl;
}

//Function to set the date
void Date::setDate(int m, int d, int y)
{
    if (isDatesCorrect(m,d,y))
    {
        month = m;
        day = d;
        year = y ;
    }
    else
    {
        cout << "Dates are in incorrect.\nPlease enter them again";
    }
}

//Function to check if date is in the correct format 
bool Date::isDatesCorrect(int m, int d, int y) //takes in day(d), month(m), year(y)
{                                               // and returns boolean value indicating if it's in correct format or not

            //Checks month        //Checks day          //Checks year
    return (isMonthCorrect(m) && isDayCorrect(d,m) && isYearCorrect(y));
}

//Function to check if month is in correct bounds
bool Date::isMonthCorrect(int m)
{
    return ( (m >= 1) && (m <= 12) ); //Checks if month is between 1 and 12
}

//Function to check if Day is in correct bounds
bool Date::isDayCorrect(int d, int m)
{
    return ((d >= 1) && (d <= daysInMonth[m-1])); //Checks if day is between 1 and the days in that month
}

//Function to check if Year is in correct bounds
bool Date::isYearCorrect(int y)
{
    return ((y >= 1901) && (y <= 2038)); //Checks if year is between the 1901 and 2038 (based on the Year 2038 Problem)
}

//Function to get the name of the month
string Date::getMonthName()
{
    return (monthNames[month-1]); //returns the name of the month from monthNames at position month-1
}

//Function to get the name of the month based on the user's input
string Date::getMonthName(int m)
{
    if (isMonthCorrect(m)) //First it checks if the month is in the correct forma
    {
        return (monthNames[m-1]);//returns the name of the month from monthNames at position month-1
    }
    else
    {
        cout << "Month is not between 1 and 12" << endl; //Error message if m is not in the correct format
    }
}

int Date::getYearsDaysBetween(Date DateVal)
{
    int sum=0;

    if (isYearDiff(DateVal))
    {
        cout << "Year was different";
        sum += getYearDiff(DateVal);
    }
    else
    {
        sum=0;
    }
    return sum;
}

int Date::getMonthsDaysBetween(Date DateVal)
{
    int sum=0;

    if (month != DateVal.month)
    {
        cout << "Month was different";
        if (month < DateVal.month)
        {
            sum += daysInMonth[month] - day;
            sum += DateVal.day;

            for (int i = month +1 ; i < DateVal.month -1; i++) //Going from month to DateVal.month, not inlcuding either
            {
                sum += daysInMonth[i];
            }
        }
        else if (DateVal.month < month)
        {
            sum += daysInMonth[DateVal.month] - DateVal.day;
            sum+= day;

            for (int i = DateVal.month; i < month; ++i)
            {
                sum+= daysInMonth[i];
            }
        }
    }

    else if(day != DateVal.day)
    {
        sum += abs(day - DateVal.day);
    }
    else
    {
        sum=0;
    }
    return sum;
}

int Date::getDaysBetween(Date DateVal)
{
    cout << "Days between ran";
    int sum = 0;

    sum+= getYearsDaysBetween(DateVal);
    sum+= getMonthsDaysBetween(DateVal);
}

bool Date::isYearDiff(Date DateVal)
{
    return (year != DateVal.year);
}

bool Date::isMonthDiff(Date DateVal)
{
    return (month != DateVal.month);
}

int Date::getYearDiff(Date DateVal)
{
    return abs(year- DateVal.year)*365;
}

int Date::getMonthDiff(Date DateVal)
{
    int sum = daysInMonth[month] - day + DateVal.day;

    if (month < DateVal.month)
    {
        for (int i = month +1 ; i < DateVal.month -1; i++) //Going from month to DateVal.month, not inlcuding either
        {
            sum += daysInMonth[i];
        }
    }
    else
    {
        for (int i = DateVal.month +1; i < month-1; i++ )
        {
            sum += daysInMonth[i];
        }
    }

    return sum;
}

我有几个问题:

1)我在哪里搞砸了,我该如何解决它,以及如何防止自己再次发生这种情况?

2)你能对我的代码发表什么评论?难以阅读吗?如果是这样,我该如何解决?我是C ++的新手,所以任何反馈都很受欢迎!

我还应该提一下,当我删除该行时程序运行正常:

d1.getDaysBetween(d2);

错误是否已在功能中本地化?我如何具体找到我弄乱的地方?我已经尝试过在这里和那里放置打印语句,似乎没有调用该函数!

再次感谢您的帮助!

0 个答案:

没有答案