title: Getting error: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid. No idea what to do
body: <p>I'm working on making a class "Date" which works on dates, and it's giving me this error: </p>
  
  <pre><code>terminate called after throwing an instance of 'std::logic_error'   
  what():  basic_string::_M_construct null not valid
  </code></pre>
  
  <p>I tried looking online for solutions, but I couldn't find any strings where this would have happened.</p>
  
  <p>Here is my code:</p>
  
  <p>The Test File: </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "Date.h"
  using namespace std;
  
  int main()
  {
      Date d1(2,3,2004), d2(3,5,2005);
  
      cout &lt;&lt; "Date 1 is: ";
      d1.printDate();
  
      cout &lt;&lt; "Date 2 is: ";
      d2.printDate();
  
      cout &lt;&lt; "The month name for Date 1 is: " &lt;&lt; d1.getMonthName() &lt;&lt; endl;
      cout &lt;&lt; "The month name for Date 2 is: " &lt;&lt; d2.getMonthName() &lt;&lt; endl;
  
      cout &lt;&lt; "The month name when Date 1's month is 7 is: " &lt;&lt; d1.getMonthName(7) &lt;&lt; endl;
  
      cout &lt;&lt; d1.getDaysBetween(d2);
      return 0;
  }
  </code></pre>
  
  <p>The Header File:</p>
  
  <pre><code>/* date.h */
  #ifndef DATE_H_
  #define DATE_H_
  
  #include &lt;array&gt;
  #include &lt;string&gt;
  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&lt;string, 12&gt; monthNames={"January","February","March","April","May","June","July","August","September","October","November","December"};
      const array&lt;int, 12&gt; 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_ */
  </code></pre>
  
  <p>The Class:</p>
  
  <pre><code>/* date.cpp */
  #include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #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 &lt;&lt; month &lt;&lt; "/" &lt;&lt; day &lt;&lt; "/" &lt;&lt; year &lt;&lt; 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 &lt;&lt; "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) &amp;&amp; isDayCorrect(d,m) &amp;&amp; isYearCorrect(y));
  }
  
  //Function to check if month is in correct bounds
  bool Date::isMonthCorrect(int m)
  {
      return ( (m &gt;= 1) &amp;&amp; (m &lt;= 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 &gt;= 1) &amp;&amp; (d &lt;= 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 &gt;= 1901) &amp;&amp; (y &lt;= 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 &lt;&lt; "Month is not between 1 and 12" &lt;&lt; endl; //Error message if m is not in the correct format
      }
  }
  
  int Date::getYearsDaysBetween(Date DateVal)
  {
      int sum=0;
  
      if (isYearDiff(DateVal))
      {
          cout &lt;&lt; "Year was different";
          sum += getYearDiff(DateVal);
      }
      else
      {
          sum=0;
      }
      return sum;
  }
  
  int Date::getMonthsDaysBetween(Date DateVal)
  {
      int sum=0;
  
      if (month != DateVal.month)
      {
          cout &lt;&lt; "Month was different";
          if (month &lt; DateVal.month)
          {
              sum += daysInMonth[month] - day;
              sum += DateVal.day;
  
              for (int i = month +1 ; i &lt; DateVal.month -1; i++) //Going from month to DateVal.month, not inlcuding either
              {
                  sum += daysInMonth[i];
              }
          }
          else if (DateVal.month &lt; month)
          {
              sum += daysInMonth[DateVal.month] - DateVal.day;
              sum+= day;
  
              for (int i = DateVal.month; i &lt; 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 &lt;&lt; "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 &lt; DateVal.month)
      {
          for (int i = month +1 ; i &lt; DateVal.month -1; i++) //Going from month to DateVal.month, not inlcuding either
          {
              sum += daysInMonth[i];
          }
      }
      else
      {
          for (int i = DateVal.month +1; i &lt; month-1; i++ )
          {
              sum += daysInMonth[i];
          }
      }
  
      return sum;
  }
  </code></pre>
  
  <p>I have a few questions:</p>
  
  <p>1) Where am I messing up, how do I fix it, and how do I prevent myself from having that happen again?</p>
  
  <p>2) What can you comment about my code? Is it difficult to read? If so, how do I fix it? I'm new to C++, so any feed back is appreciated!</p>
  
  <p>I should also mention that the program works fine when I remove the line: </p>
  
  <pre><code>d1.getDaysBetween(d2);
  </code></pre>
  
  <p>Is the error localised in function? How do I find out specifically where I'm messing up? I've tried putting print statements here and there and it seems that the function isn't called upon!</p>
  
  <p>Thanks again for your help!</p>