2014-10-31 41 views
-1

好的,我知道這被新手C++用戶問了很多,因爲我已經閱讀了大量的帖子,但即使在閱讀答案後,我也沒有看到我做錯了什麼。代碼很長,所以......儘可能寬容。轉發聲明錯誤駕駛我堅果

#include "dateType.h" 

using namespace std; 

void dateType::setDate(int y, int m, int d) 
{ 
    y = year; 
    m = month; 
    d = day; 
} 

int dateType::getYear() const 
{ 
    return year; 
} 

int dateType::getMonth() const 
{ 
    return month; 
} 

int dateType::getDay() const 
{ 
    return day; 
} 

void dateType::getDate(int& year, int& month, int& day) const 
{ 
    return (year, month, day); 
} 

bool dateType::operator==(const dateType& otherDate) const 
{ 
    return (getYear() == otherDate.getYear() && getMonth() == otherDate.getMonth() 
     && d == otherDate.getDay()); 
} 

bool dateType::operator!=(const dateType& otherDate) const 
{ 
    return !(*this == otherDate); 
} 

bool dateType::operator<(const dateType& otherDate) const 
{ 
    return (getYear() < otherDate.getYear() || getMonth() < otherdate.getMonth() 
     || getDay() < otherDate.getDay()); 
} 

bool dateType::operator>(const dateType& otherDate) const 
{ 
    return (getYear() > otherDate.getYear() || getMonth() > otherDate.getMonth() 
     || getDay() > otherDate.getDay()); 
} 

bool dateType::operator<=(const dateType& otherDate) const 
{ 
    return *this == otherDate || *this < otherDate; 
} 

bool dateType::operator>=(const dateType& otherDate) const 
{ 
    return *this == otherDate || *this > otherDate; 
} 

ostream& operator<<(ostream& out, const dateType& d) 
{ 
    out << dateType.getYear() << "-" << dateType.getMonth() << "-" << dateType.getDay(); 
    return out; 
} 

istream& operator>>(istream& in, dateType& d) 
{ 
    //Variables 
    int year, 
     month, 
     day; 
    char dummy; 

    in >> year >> dummy >> month >> dummy >> day; 
    d.setDate(year, month, day); 
    return in; 
} 

dateType::dateType() 
{ 
    year = 0000; 
    month = 00; 
    day = 00; 
} 

dateType::dateType(int y, int m, int d) 
{ 
    setDate(y, m, d); 
} 

我的.h文件是:

#ifndef _DATETYPE_H_ 
#define _DATETYPE_H_ 

#include <iostream> 

using namespace std; 

class dateType 
{ 
    public: 
    /* Method: Default contructor 
    * Description: Constructs a new dateType object 
    * Pre-conditions: None 
    * Post-conditions: dateType object created and initialized to a date of 
    * '0000-00-00' (format yyyy-mm-dd) 
    * Method input: Year (int), Month (int), Day (int) 
    * Method output: None 
    */ 
    dateType(); 

    /* Method: Constructor 
    * Description: Constructs a new dateType object 
    * Pre-conditions: None 
    * Post-conditions: dateType object create and initialized to a date of 
    * format yyyy-mm-dd 
    * Method input: year (int), month (int), day (int) 
    * Method output: None 
    */ 
    dateType(int, int, int); 

    /* Method: setDate 
    * Description: Sets the date of a dateType object 
    * Pre-conditions: dateType object has been initialized 
    * Post-conditions: new year, month and day has been set for dateType 
    * object 
    * Method input: year(int), month(int), day(int) 
    * Method output: None 
    */ 
    void setDate(int y, int m, int d); 

    /* Method: getYear 
    * Descritpion: Returns the year of the dateType object 
    * Pre-conditions: dateType object exists 
    * Post-conditions: dateType object year has been returned 
    * Method input: None 
    * Method output: Year of the dateType (int) 
    */ 
    int getYear() const; 

    /* Method: getMonth 
    * Descritpion: Returns the month of the dateType object 
    * Pre-conditions: dateType object exists 
    * Post-conditions: dateType object month has been returned 
    * Method input: None 
    * Method output: Month of the dateType object (int) 
    */ 
    int getMonth() const; 

    /* Method: getDay 
    * Description: Returns the day of the dateType object 
    * Pre-conditions: dateType object exists 
    * Post-conditions: dateType object month has been returned 
    * Method input: None 
    * Method output: Day of the dateType object (int) 
    */ 
    int getDay() const; 

    /* Method: getDate 
    * Description: Returns the year, month, and day of the dateType object 
    * Pre-conditions: dateType object exists 
    * Post-conditions: dateType object year, month and day has been returned 
    * Method input: None 
    * Method output: year (int), month(int), and day(int) of the dateType 
    * object 
    */ 
    void getDate() const; 

    private: 
    int year; 
    int month; 
    int day; 
}; 

ostream& operator<<(ostream&, const dateType); 
#endif 

我還在上.h文件的工作,但我試圖測試只是類的初始化中的main.cpp文件。

dateType first_date(2014, 10, 31); 

我得到了一些錯誤,但最先是: dateType.cpp:13:43:錯誤:無效的使用不完全類型 '類dateType' 無效dateType ::的setDate(INT Y的, int m,int d)

請告訴我我錯過了什麼。

非常感謝。

+0

在'setDate':'Y =年; m =月; d =日;'笏? – user657267 2014-10-31 05:08:50

+0

哎呀,一點小小的疏漏。謝謝。 – Blackwell 2014-10-31 05:09:05

+0

@Blackwell如果你使用的是gcc或clang,用'-Wall -Wextra -Wpedantic-WeffC++'把警告變成11,它們可以幫助你捕捉這樣的簡單錯誤。 – user657267 2014-10-31 05:09:58

回答

0

我已經糾正了所有錯誤檢查以下文件

,以檢查其鏈接http://ideone.com/nfdYo5

您的.H文件

#ifndef _DATETYPE_H_ 
#define _DATETYPE_H_ 

#include <iostream> 

using namespace std; 

class dateType 
{ 
    public: 
    /* Method: Default contructor 
    * Description: Constructs a new dateType object 
    * Pre-conditions: None 
    * Post-conditions: dateType object created and initialized to a date of 
    * '0000-00-00' (format yyyy-mm-dd) 
    * Method input: Year (int), Month (int), Day (int) 
    * Method output: None 
    */ 
    dateType(); 

    /* Method: Constructor 
    * Description: Constructs a new dateType object 
    * Pre-conditions: None 
    * Post-conditions: dateType object create and initialized to a date of 
    * format yyyy-mm-dd 
    * Method input: year (int), month (int), day (int) 
    * Method output: None 
    */ 
    dateType(int, int, int); 

    /* Method: setDate 
    * Description: Sets the date of a dateType object 
    * Pre-conditions: dateType object has been initialized 
    * Post-conditions: new year, month and day has been set for dateType 
    * object 
    * Method input: year(int), month(int), day(int) 
    * Method output: None 
    */ 
    void setDate(int y, int m, int d); 

    /* Method: getYear 
    * Descritpion: Returns the year of the dateType object 
    * Pre-conditions: dateType object exists 
    * Post-conditions: dateType object year has been returned 
    * Method input: None 
    * Method output: Year of the dateType (int) 
    */ 
    int getYear() const; 

    /* Method: getMonth 
    * Descritpion: Returns the month of the dateType object 
    * Pre-conditions: dateType object exists 
    * Post-conditions: dateType object month has been returned 
    * Method input: None 
    * Method output: Month of the dateType object (int) 
    */ 
    int getMonth() const; 

    /* Method: getDay 
    * Description: Returns the day of the dateType object 
    * Pre-conditions: dateType object exists 
    * Post-conditions: dateType object month has been returned 
    * Method input: None 
    * Method output: Day of the dateType object (int) 
    */ 
    int getDay() const; 

    /* Method: getDate 
    * Description: Returns the year, month, and day of the dateType object 
    * Pre-conditions: dateType object exists 
    * Post-conditions: dateType object year, month and day has been returned 
    * Method input: None 
    * Method output: year (int), month(int), and day(int) of the dateType 
    * object 
    */ 
    dateType getDate() const; 
    bool operator==(const dateType& otherDate) const; 
    bool operator!=(const dateType& otherDate) const; 
    bool operator<(const dateType& otherDate) const; 
    bool operator>(const dateType& otherDate) const; 
    bool operator<=(const dateType& otherDate) const; 
    bool operator>=(const dateType& otherDate) const; 
    ostream& operator<<(ostream& out); 
    istream& operator>>(istream& in); 

    private: 
    int year; 
    int month; 
    int day; 
}; 
#endif 

您的CPP文件

#include "dateType.h" 

void dateType::setDate(int y, int m, int d) 
{ 
    year = y; 
    month = m; 
    day = d; 
} 

int dateType::getYear() const 
{ 
    return year; 
} 

int dateType::getMonth() const 
{ 
    return month; 
} 

int dateType::getDay() const 
{ 
    return day; 
} 

dateType dateType::getDate() const 
{ 
return dateType(year, month, day); 
} 

bool dateType::operator==(const dateType& otherDate) const 
{ 
    return (getYear() == otherDate.getYear() && getMonth() == otherDate.getMonth() 
     && getDay() == otherDate.getDay()); 
} 

bool dateType::operator!=(const dateType& otherDate) const 
{ 
    return !(*this == otherDate); 
} 

bool dateType::operator<(const dateType& otherDate) const 
{ 
    return (getYear() < otherDate.getYear() || getMonth() < otherDate.getMonth() 
     || getDay() < otherDate.getDay()); 
} 

bool dateType::operator>(const dateType& otherDate) const 
{ 
    return (getYear() > otherDate.getYear() || getMonth() > otherDate.getMonth() 
     || getDay() > otherDate.getDay()); 
} 

bool dateType::operator<=(const dateType& otherDate) const 
{ 
    return *this == otherDate || *this < otherDate; 
} 

bool dateType::operator>=(const dateType& otherDate) const 
{ 
    return *this == otherDate || *this > otherDate; 
} 

ostream& dateType::operator<<(ostream& out) 
{ 
    out << this->getYear() << "-" << this->getMonth() << "-" << this->getDay(); 
    return out; 
} 

istream& dateType::operator>>(istream& in) 
{ 
    //Variables 
    int year, 
     month, 
     day; 
    char dummy; 

    in >> year >> dummy >> month >> dummy >> day; 
    this->setDate(year, month, day); 
    return in; 
} 

dateType::dateType() 
{ 
    year = 0000; 
    month = 00; 
    day = 00; 
} 

dateType::dateType(int y, int m, int d) 
{ 
    setDate(y, m, d); 
} 

複製以及過去和樂趣

它有很多錯誤和錯字的... :)

0

你想要返回什麼?

void dateType::getDate(int& year, int& month, int& day) const 
{ 
    return (year, month, day); <<<<<<<<<<<<< 
} 

請更改爲

dateType dateType::getDate(int& year, int& month, int& day) 
{ 
    return dateType(year, month, day); 
} 
+0

固定。我不確定如何返回那個。應該已經看到了。 – Blackwell 2014-10-31 05:14:50

0

你的setDate功能是錯誤的。它應該是

void dateType::setDate(int y, int m, int d) 
    { 
     year = y; 
     month = m; 
     day = d; 
    } 

而且你不需要這個聲明中的.h

ostream& operator<<(ostream&, const dateType);