2013-12-17 26 views
1

我試圖創建自己的類,其中包含三個變量:日,月和年。我添加兩個運算符進行比較。這裏是我的頭文件和CPP文件:無法創建我自己的日期類

頁眉:

#ifndef DATE_H 
#define DATE_H 

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <vector> 
#include <algorithm> 
#include <iterator> 
#include <array> 
using namespace std; 

class Date { 
public: 
    int day; 
    int month; 
    int year; 
    Date(int m, int d, int y); 
    bool operator< (const Date &) const; 
    bool operator== (const Date &) const; 
} 

#endif 

CPP:

#include "stdafx.h" 
#include "date.h" 

Date::Date(int m, int d, int y) 
    :day(d),month(m),year(y){} 

bool Date::operator< (const Date & d2) const 
{ 
    bool result; 
    if(year<d2.year){ 
     result=true; 
    } 
    else if (year==d2.year&&month<d2.month){ 
     result=true; 
    } 
    else if (month==d2.month&&day<d2.day){ 
     result = true; 
    } 
    else{ 
     result = false; 
    } 
    return result; 
} 

bool Date::operator== (const Date & d2) const 
{ 
    return (year==d2.year)&&(month==d2.month)&&(day==d2.day); 
} 

的錯誤是

錯誤C2533:「日期:: {}構造函數':構造函數不允許返回類型

謝謝你的幫助!

+3

在類聲明結尾缺少分號。 – David

+1

爲什麼你需要頭文件中的任何包含文件?你沒有在標題中使用它們中的任何一個! –

+0

您的cpp文件沒有使用額外的包含文件。我強烈建議刪除* stdafx.h *,因爲它會導致更多的小文件問題,而不是解決問題。 –

回答

2

類定義在最後缺少分號。


其他評論:

  • 爲了避免名稱衝突(例如用std::distance),不要把using namespace std;在全局命名空間中的標題。

  • <stdafx.h>是一個非標準頭文件,在您的Visual Studio項目中定義,它使得代碼依賴於Visual Studio。您可以通過關閉項目設置中的「預編譯頭文件」來避免它。

0

類或結構的聲明必須以';'結尾。 '