[英]int conversion operator 'Class followed by int is illegal' return type of main should be 'int' not 'class'
I'm teaching myself C++ right now from a book and one of the exercises is to program a class Date that converts a date it holds into a unique integer. 我现在正在从一本书中教自己C ++,其中一项练习是编写一个类Date,以将其保存的日期转换为唯一的整数。 However I can't figure out this error I'm getting when I run my program.
但是,我无法确定运行程序时遇到的错误。 I'm programming on C++ 2010.
我正在使用C ++ 2010进行编程。
The errors are: 错误是:
error C2628: 'Date' followed by 'int' is illegal (did you forget a ';'?)
error C3874: return type of 'main' should be 'int' instead of 'Date'
What's odd is I tried to change my main to simply "return 0;" 奇怪的是,我试图将主代码更改为简单的“返回0”; and the above errors still occur.
并且仍然会发生上述错误。 Any ideas?
有任何想法吗?
Here's my code: 这是我的代码:
#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
}
You need to add a ;
您需要添加一个
;
after the class definition. 在类定义之后。
I hope that the following code answers your query! 我希望以下代码可以回答您的查询!
#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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.