2012-11-25 26 views
0

我在教自己C++現在從一本書,其中一個練習是編程一個類日期將其保存的日期轉換爲唯一的整數。但是我無法弄清楚我運行程序時遇到的這個錯誤。我對C++ 2010int轉換運算符類後跟int是非法的返回類型主要應該是'int'而不是'class'

編程的錯誤是:

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 
} 

回答

6

您需要的類定義後添加;

+1

洛爾該死的,我這樣的小白 –

+0

我很慚愧地給予好評這個:-) –

+0

我很慚愧地問這個 –

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; 
} 
相關問題