2012-03-11 16 views
-3

以前は、JavaプログラムをC++に変換する作業がありました。C++の機能の問題

私はこれを行いました。私には意味をなさないような奇妙なエラーが発生しました。

プログラムは3つのファイルで構成されています。 main.ccp、date.ccp、およびdate.h。

main.ccp

#include <cstdlib> 
#include <iostream> 

#include "Date.h" 

using namespace ::std; 

string weekday(Date date); 

int main() { 

    int day, month, year; 

    cout << "What date (d m y)? "; 

    cin >> day >> month >> year; 

    Date event = Date(day, month, year); 
    cout << ("That was a " + weekday(event)); 
    return 0; 
} 

string weekday(Date date) { 
    const string days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", 
     "Thursday", "Friday", "Saturday"}; 

    Date trial = Date(1, 1, 1); 
    int weekday = 6; 

    if (Date::precedes(trial)) { 
     return "Mysteryday"; 
    } else { 
     while (trial.precedes(date)) { 
      trial.advance(); 
      weekday = (weekday + 1) % 7; 
     } 
     return days[weekday]; 
    } 
} 

Date.ccp

#include "Date.h" 

Date::Date(int day, int month, int year) { 
     day_ = day; 
     month_ = month; 
     year_ = year; 
    } 

    int Date::getDay() { 
     return day_; 
    } 

    void Date::setDay (int day) { 
     day_ = day; 
    } 

    int Date::getMonth() { 
     return month_; 
    } 

    void Date::setMonth (int month) { 
     month_ = month; 
    } 

    int Date::getYear() { 
     return year_; 
    } 

    void Date::setYear (int year) { 
     year_ = year; 
    } 

    bool Date::isLeapYear() { 

     bool lear = false; 

     if (this->year_ <= 1752) { 
      if (this->year_ % 4 == 0) { 
       lear = true; 
      } 
     } 
     else { 
      lear = false; 
     } 
     if (this->year_ > 1752) { 
      if (this->year_ % 4 == 0 && this->year_ % 100 != 0) { 
       lear = true; 
      } 
      else if (this->year_ % 4 == 0 && this->year_ % 100 == 0 && this->year_ % 400 == 0) { 
       lear = true; 
      } 
      else { 
       lear = false; 
      } 
     } 
     return lear; 
    } 

    int Date::daysInMonth() { 
     // "30 days hath September ... " 
     switch (this->month_) { 
     case 9 : 
     case 4 : 
     case 6 : 
     case 11 : 
      return 30; 
     default : 
      return 31; 
     case 2 : 
      return this->isLeapYear() ? 29 : 28; 
     } 
    } 

    void Date::advance() { 
     this->day_; 

     if (this->day_ == 3 && this->month_ == 9 && this->year_ == 1752) { 
      day_ = 14; 
      month_ = 9; 
      year_ = 1752; 
     } 

     if (this->day_ > this->daysInMonth()) { 
      this->day_ = 1; 
      this->month_++; 
     } 
     if (this->month_ > 12) { 
      this->month_ = 1; 
      this->year_++; 

     } 
    } 

    bool Date::precedes (Date date) { 
     return this->year_ < date->year_ 
      || this->year_ == date->year_ && this->month_ < date->month_ 
      || this->year_ == date->year_ && this->month_ == date->month_ && this->day_ < date->day_; 
    } 

Date.h

#ifndef DATE_H 
#define DATE_H 

class Date { 
public: 
    Date (int day, int month, int year); 
    int getDay(); 
    void setDay(); 
    int getMonth(); 
    void setMonth(); 
    int getYear(); 
    void setYear(); 
    bool isLeapYear(); 
    int daysInMonth(); 
    void advance(); 
    bool precedes(Date date); 

private: 
     int day_; 
     int month_; 
     int year_; 
}; 

#endif /* DATE_H */ 

私はの多くを取得しているように見えますコンパイル時に同じエラーが発生します。

Date.cpp:95:97のエラー:の基本オペランド「 - >」が非ポインタ型「日付」を持っている

は私が正しいかではない宣言をやったかはわかりません。

+0

'year_'、' day_'、 'month_'を単一の' int [3] 'にすると、' lexicographical_compare'のようなものを使うことができます。 –

+0

@Andrew - それをメモしないで質問からエラーを取り除くのはうまくいきません。後でここに来るのは私たちにとって本当に混乱しています。 –

答えて

3

クラスの終了後にセミコロンを入力するのを忘れた}

+0

ああ、私はほとんど信じられない。ハハ、ほとんどのエラーを修正。ありがとうございました。ああ私の不注意。 – Sasstraliss

0

Dateのクラス定義は、セミコロンで終了しません。 #include main.cppにこのファイルがあるので、ヘッダーの後の最初のステートメントは、この構文エラーのために解析されないステートメントです。したがって、間違った場所でエラーが発生します。

+0

Date.precedesという関数を呼び出すと、まだエラーが発生しているようです。 main.cpp:31:28:エラー: 'Date :: precedes(Date&)'の呼び出しに一致する関数がありません Date.h:16:10:メモ:候補はboolですDate :: precedes() – Sasstraliss

+0

@ AndrewBooth:これは別のエラーですあなたは、 'boolが先行する();'というパラメータを取っていないと '先行する 'と宣言しています。 –

+0

mainからの入力を使用する宣言はどうすればよいですか? – Sasstraliss