私は今、本からC ++を教えています。演習のひとつは、保持している日付を一意の整数に変換するクラスDateをプログラムすることです。ただし、プログラムを実行したときに発生するこのエラーを理解できません。私はC++2010でプログラミングしています。
エラーは次のとおりです。
error C2628: 'Date' followed by 'int' is illegal (did you forget a ';'?)
error C3874: return type of 'main' should be 'int' instead of 'Date'
奇妙なことに、メインを単に「0を返す」に変更しようとしました。上記のエラーは引き続き発生します。何か案は?
これが私のコードです:
#include "stdafx.h"
#include <iostream>
using namespace std;
class Date{
private:
int day, month, year; //declaring variables
public:
//declare constructor
Date(int inputDay=1, int inputMonth=1, int inputYear=2012)
:day(inputDay), month(inputMonth),year(inputYear){};
// declare conversion operator for integers
operator int(){
return year*10000+month*100+day;
}
}
int main() {
Date today(25,11,2012);
return today;
//doesn't matter if I delete above 2 lines and write return 0; both errors still occur
}