我試圖創建自己的類,其中包含三個變量:日,月和年。我添加兩個運算符進行比較。這裏是我的頭文件和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:「日期:: {}構造函數':構造函數不允許返回類型
謝謝你的幫助!
在類聲明結尾缺少分號。 – David
爲什麼你需要頭文件中的任何包含文件?你沒有在標題中使用它們中的任何一個! –
您的cpp文件沒有使用額外的包含文件。我強烈建議刪除* stdafx.h *,因爲它會導致更多的小文件問題,而不是解決問題。 –