-1

我是一名学生,试图创建一个代码来输入 2 种不同格式的日期并使用重载的构造函数来区分它们,但我被卡住了,因为我在头文件中声明的变量没有在构造函数之外保持声明,这是一个我在
main(lab6)之前从未遇到过的问题

 #include <cstdlib>
 #include <iostream>
 #include "date.h"
 #include "test.h"
 #include <string>
using namespace std;

 int main(int argc, char *argv[])
{
int month;
int year;
int day;
std::string monthstr;
int i=0;
while(i != 3)
{
   std::cout << "Enter 1 for format: MM/DD/YYYY\nEnter 2 for format: Month DD,     YYYY\nEnter 3 for exit\nChoice:";    
  std::cin >> i;   
  switch(i)
  {
   case 1:
    {
    std::cout << "enter month(1-12):";
    std::cin >> month;              
    std::cout << "enter day of month:";
    std::cin >> day;
    std::cout << "enter year:";
    std::cin >> year;                   
    test test( month, day, year ) ; 

      test(month, day, year);      
        month = test.testdate();
        break ;}  
    case 2:
       {  
          std::cout << "enter month name:";
    std::cin >> monthstr;              
    std::cout << "enter day of month:";
    std::cin >> day;
    std::cout << "enter year:";
    std::cin >> year;                   
    **here is the problem **
     test test( monthstr, day, year ) ;     
     month = test.testdate();
     break;}     
           }
      // month = test.testdate(); 
       date date(month, day, year);
       date.datedis();

       }

      system("PAUSE");
      return EXIT_SUCCESS;
     }

test.ccptests id 日期是真实的*这里是问题* _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ _ __ _ __ _ ___

#include <iostream>
#include <iomanip>
#include "test.h"
#include "date.h"
#include <string>
#include <stdexcept>
#include <locale> 
using namespace std;

test::test(int m, int d, int y)
{
mon = m;
da = d;
ye = y;}

test::test(std::string m, int d, int y)
{
da = d;
ye = y  ;                   
std::string months[] = { "january", "february", "march", "april", "may", "june",   "july", "august", "september", "october", "november", "december" };                    
 int i = 0;
 while (i != 12 || i !=13)
  {i++;
  std::locale loc;
  if (months[i-1] == std::tolower(m,loc ) )   
     {mon = i;
     i = 13;}
  if (i!=13)
  {throw invalid_argument("month is worng");}}}

  int testdate()
  {
    if (mon < 0 && mon >= 12)
       {throw invalid_argument( "month must be betwen 1-12");}

    if (ye < 0 )
    {throw invalid_argument( "year must be real");}

    if (mon == 2 && da == 29 && (ye % 400 != 0 || (ye % 4 != 0 && ye % 100 == 0)))
    {throw invalid_argument("not a leap year");}

    int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if(da < 0 || da > days[mon-1])
    {throw invalid_argument("day not correct");}
    return mon;
    //  date date(mon, da, ye);}

test.h 头文件_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ____

class test
#include <string>
//#include "date.h"

{
  public:
    test(int, int, int );
    test(std::string, int, int );
    int testdate();
   private:
   int mon;
   int da;
   int ye;
   std::string mons;

   };

date.ccp 在此处显示日期* 问题* _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _

#include <iostream>
#include <iomanip>
#include "test.h"
#include "date.h"
#include <string>
#include <stdexcept>
using namespace std;

date::date(int m, int d, int y)
{ month = m;
 daay = d;
 year = y;}
  **problem here**
void datedis()
{std::string months[] = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };

   std::cout << "MM/DD/YYYY: "<< month << "/" << daay << "/" << year <<"\nmonth DD,YYYY: "<< months[month-1] << " " << daay <<","<< year;                  
        }

date.h 标题为 date_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ___

#include <string>
 class date
 {
  public:
      date(int, int, int );
    void datedis();
   private:
   int month;
   int daay;
   int year;      
  };
4

1 回答 1

0

您只是忘记将 datedis 声明为类日期的方法。

无效的日期()

在文件中 date.cpp 应该是

无效日期::datedis()

于 2013-10-10T19:11:22.110 回答