晕倒哦.我的代码很多错误的.我只是在根据错误一个一个去改正.但这个错误引起了绝大部分的错误.错得太多了,见笑了.希望能解答为什么说意外的"class"CLeapYear
#include <iostream>
#include <string.h>
using namespace std;
struct Syear
{
int start,end;
}
//10
class CLeapYear
{
private:
Syear time;
char *p_leap_year;
char *p_time_name;
static int total_year_object;
static char no_of_leap_year;
//20
public:
CLeapYear(const Syear &r_time_val,char time_name[]);
int setyear(const int &r_year,const Syear time);
bool isLeap(const &r_year) const;
void findLeapYear();
void print() const;
~CLeapYear()
{
cout<<"destructing leap_year"<<this->p_leap_year<<endl;
delete p_leap_year;//30
}
};
CLeapYear::CLeapYear(const Syear &r_time_val,char time_name[])
{
cout<<"Constructor with 2 parameters is called"<<endl;
time=r_time_val;
p_time_name=new char[strlen(time_name)+1];
if(time_name==NULL)
exit(1);//40
}
int CLleapYear::setyear(const int &r_year,const Syear time)
{
char ch;
ch=cin.get();
cout<<"请选择:输入1,判断将要输入的某个年份是否闰年;输入2,输入起始年份列出期间的所有闰年.\n";
if(ch==1)
cout<<"请输入一个要判断的年份";
cin>>r_year;//50
if(ch==2)
cout<<"请输入起始年份";
cin>>time.start>>time.end;
}
bool CLleapYear::isLeap(const &r_year)
{//60
if(r_year%4 == 0 &&r_ year%100 != 0)
return true;
if(r_year%400 == 0)
return true;
return false;
}
int CLeapYear::findLeapYear()//70
{
int year;
for(year=start;year<=end;year++)
{
if((year%4 == 0 && year%100 != 0) || year%400 == 0)
no_of_leap_year++;
p_leap_year=new char[no_of_leap_year+1];
strcpy(p_leap_year,year);
return *p_leap_year;//80
}
}
int CLeapYear::print()
{
cout<<"The leap year from"<<start<<"to"<<end<<"are"<<p_leap_year++;
cout<<endl;
}
//90
int CLeapYear::total_year_object=0;
int CLeapYear::no_of_leap_year=0;
void main()
{
Syear time1={1904,2007},time2={1876,1956};
CLeapYear leapyear1(time1,"first"),leapyear2=(time2,"second");
//100
leapyear1.findLeapYear();
leapyear1.print();
leapyear2.findLeapYear();
leapyear2.print();
delete p_leap_year;
delete p_time_name;
}
错误如下(只列一部分):
d:\My Documents\Visual Studio Projects\563\256\200431046.cpp(12): error C2236: 意外的“class”“CLeapYear”
d:\My Documents\Visual Studio Projects\563\256\200431046.cpp(12): error C2143: 语法错误 : 缺少“;”(在“{”的前面)
d:\My Documents\Visual Studio Projects\563\256\200431046.cpp(12): error C2447: “{” : 缺少函数标题(是否是老式的形式表?)
d:\My Documents\Visual Studio Projects\563\256\200431046.cpp(34): error C2653: “CLeapYear” : 不是类或命名空间名称