1

私は今、本から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
}
4

2 に答える 2

8

;クラス定義の後にを追加する必要があります。

于 2012-11-25T16:42:34.037 に答える
0

次のコードがあなたの質問に答えることを願っています!

#include <iostream.h>

class Date
{
private:
int day, month, year; //declaring variables
long int result;

public:

//declare constructor
Date(int inputDay, int inputMonth, int inputYear)
{
this.day=inputDay;
this.month=inputMonth;
this.year=inputYear;
// declare conversion operator for integers
result=year*10000+month*100+day;
cout<<"Result = "<<result<<"\n";
}
};


int main() 
{
Date today(25,11,2012);
return 0;
}
于 2012-11-25T17:34:26.957 に答える