我正在爲作業寫一個「Date」類,並且在執行一個函數時遇到問題。如何比較一個類中的兩個對象(調用對象和參數)?
這是該類的頭文件。
class Date
{
public:
Date(); // Constructor without parameters
Date(int m, int d, int y); // Constructor with parameters.
// accessors
int GetMonth(); // returns the size of the diamond
int GetDay();
int GetYear();
// mutators
bool Set(int m, int d, int y);
bool SetFormat(char f);
// standard input and output routines
void Input();
void Show();
void Increment(int numDays = 1);
int Compare(const Date& d);
private:
int month, // month variables
day, // day variable
year; // year variable
char format;
};
我想說明的成員函數是INT比較(常量日期& d)功能。我需要此函數來比較兩個Date對象(調用對象和 參數),並且應該返回:-1如果調用對象按時間順序排在前 ,如果對象是相同日期則爲0,如果參數對象爲 按時間順序排列。
我試着做一個簡單的if語句與==操作符,但我得到錯誤。
if (d1 == d2)
cout << "The dates are the same";
return (0);
對象被創建後,函數應該被稱爲像這樣d1.Compare(D2)
預先感謝您!
謝謝,這正是我想要做的。 – 2010-02-03 20:32:17