标题:求教"C++创建类出现的错误"
取消只看楼主
白衣吹水
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-4-10
 问题点数:0 回复次数:3 
求教"C++创建类出现的错误"

我创建类时,出现了"错误的class"的错误提示.我创建的类如下:

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
}
};



究竟哪里出现错误了?现在我这个程序大部分错误都由这个错误引起的,疯了.

请教各位高手.

搜索更多相关主题的帖子: class private public 
2007-05-10 17:44
白衣吹水
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-4-10
得分:0 

晕倒哦.我的代码很多错误的.我只是在根据错误一个一个去改正.但这个错误引起了绝大部分的错误.错得太多了,见笑了.希望能解答为什么说意外的"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” : 不是类或命名空间名称



2007-05-10 18:22
白衣吹水
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-4-10
得分:0 
请问那个意外的"class" CLeapYear是什么意思?
d:\My Documents\Visual Studio Projects\563\256\200431046.cpp(12): error C2236: 意外的“class”“CLeapYear”
2007-05-10 18:35
白衣吹水
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2007-4-10
得分:0 
找到了,谢谢.是定义结构时在后面忘了加";",加上后错误马上少了一半以上.哈哈.
2007-05-10 18:41



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-138312-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035308 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved