2017-10-28 6 views
0

これは私のCalendar.cppファイルです。'Date': 'クラス'型の再定義:ERROR(calendar.cppファイルのエラー)

#include <iostream> 
#include <string> 
#include <sstream> 
#include "Calendar.h" 

char months[12][10] = { "January", "February", "March","April","May","June","July","August","September","October","November","December" }; 

問題がクラス宣言に表示されています。

class Date { 

private: 
int dd, mm, yy; 
public: 

Date() { 
    dd = 1; mm = 1; yy = 1900; 
} 

Date(int m, int d, int y) { 

    dd = d; mm = m; yy = y; 
    bool check = checkdate(); 
    if (!check) { 
     cout << "Invalid date give. Resetting to default" << endl; 
     dd = mm = 1; yy = 1900; 
    } 
} 

bool checkdate() { 
    bool leap = checkleapyear(); 
    if (mm>12 || mm <= 0) //check months 
     return false; 

    if ((mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) && dd>31) 
     return false; 
    else if (leap && mm == 2 && dd>29) 
     return false; 
    else if (mm == 2 && dd>28) 
     return false; 
    else if (dd>30) 
     return false; 
    return true; 
} 

bool checkleapyear() { 
    if ((yy % 400 == 0) || (yy % 4 == 0 && yy % 100 != 0)) 
     return true; 
    else 
     return false; 
} 
string toString() { 
    stringstream ss; 
    ss << months[mm - 1] << " " << dd << "," << yy; 
    return ss.str(); 
} 
Date nextDate() { 
    int y, d, m; 

    if (mm == 2) { 
     bool flag = checkleapyear(); 
     if (flag) { 
      if (dd >= 29) { 
       d = 1; 
       m = 3; 
      } 
      else 
       d++; 
     } 
     else { 
      if (dd >= 28) { d = 1; m = 3; } 
      else 
       d++; 
     } 
     y = yy; 
     return Date(m, d, y); 
    } 

    if ((mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10) && dd>31) 
    { 
     m++; 
     d = 1; 
     y = yy; 
    } 
    else if (mm == 12) { m = 1; y = yy++; d = 1; } 
    else if (dd>30) { 
     m++; 
     d = 1; 
     y = yy; 
    } 
    else { 
     d = dd + 1; 
     m = mm; 
     y = yy; 
    } 

    return Date(m, d, y); 

} 

void compareDates(Date &d) 
{ 
    if (d.yy>this->yy) 
     cout << "The first date comes before the second date" << endl; 
    else if (d.yy<this->yy) 
     cout << "The second date comes before the first date" << endl; 
    else if (d.yy == this->yy) { 
     if (d.mm>this->mm) 
      cout << "The first date comes before the second date" << endl; 
     else if (d.mm<this->mm) 
      cout << "The second date comes before the first date" << endl; 
     else { 

      if (d.dd>this->dd) 
       cout << "The first date comes before the second date" << endl; 
      else if (d.dd<this->dd) 
       cout << "The second date comes before the first date" << endl; 
      else { 
       cout << "The two dates are equal" << endl; 
      } 
     } 

    } 
} 

}; 

これは私のCalendar.hファイルです。

#ifndef CALENDAR_H 
#define CALENDAR_H 
#include <iostream> 
#include <string> 
using namespace std; 

class Date { 
private: 
    int dd, mm, yy; 
public: 
    Date(); 
    Date(int m, int d, int y); 
    bool checkdate(); 
    bool checkleapyear(); 
    string toString(); 
    Date nextDate(); 
    void compareDates(Date &d); 
}; 
#endif 

「 'Date': 'class' type redefinition」というCalendar.cppファイルにエラーが発生します。クラス内のいくつかの要素を変更して動作させようとしましたが、他にも問題があるようです。

+3

?クラスとその実装はどのように使用しているC++ブックに記述されていますか?コンパイラは、文字通り問題が何であるかを伝えています。 – PaulMcKenzie

答えて

0

Calender.hファイルにクラスの宣言があるので、Calender.cppの実装は実際にクラスを赤字化しています。それを行うための正しい方法は次のようになります。スコープ解決演算子「::

あなたCalender.cppファイルを使用してクラスを参照するために、次のようになります。それ

#include <iostream> 
#include <string> 
#include <sstream> 
#include "Calendar.h" 

char months[12][10] = { "January", "February", "March","April","May","June","July","August","September","October","November","December" }; 

// your default no-args class constructor 
Date::Date(){ 
    dd = 1; mm = 1; yy = 1900; 
} 

/* 
     Another Way you could implement your constructor is by using initialisation lists (check our the second link in my answer to know more) 
     Date::Date(int m, int d, int y) : mm(m), dd(d), yy(y) { } 
*/ 

Date::Date(int m, int d, int y) { 
    // your constructor code here 
} 

bool Date::checkdate(){ 
    // your implementation here 
} 
bool Date::checkleapyear(){ 
    // your implementation here 
} 
string Date::toString(){ 
    // your implementation here 
} 
Date Date::nextDate(){ 
    // your implementation here 
} 
void Date::compareDates(Date &d){ 
    // your implementation here 
} 

だこと。あなたのクラス宣言と実装を別々にするのに本当に役立ちます。

は、あなたがよりよく理解するために、以下のこれらのリンクをチェックアウト:あなたは2 `クラスDate`定義しなければならないのはなぜ

http://www.learncpp.com/cpp-tutorial/89-class-code-and-header-files/

http://www.cppforschool.com/tutorial/separate-header-and-implementation-files.html

+0

問題を解決する場合は、これを回答としてマークしてください –

関連する問題