C++ 类学习程序编译中出现syntax error : missing ';' before '{',怎么解决
源程序:#include<iostream.h>classMyClass{public:MyClass(){x=0;cout<<"Defaultconstructorof...
源程序:
#include<iostream.h>
class MyClass
{
public:
MyClass()
{
x = 0;
cout<<"Default constructor of MyClass called!"<<endl;
}
MyClass(int i)
{
x = i;
cout<<"Constructor of MyClass called!("<<x<<")"<<endl;
}
~MyClass()
{
cout<<"Destructor of MyClass called!("<<x<<")"<<endl;
}
int GetX()
{
return x;
}
void Print()
{
cout<<x<<endl;
}
private:
int x;
};
class ExamClass()
{
public:
ExamClass()
{
val = 0;
cout<<"Default constructor of ExamClass called!"<<endl;
}
ExamClass(int i, int j, int k):member_obj2(k),member_obj1(j)
{
val = i;
cout<<"Destructor of ExamClass called!("<<val<<")"<<endl;
}
~ExamClass()
{
cout<<"Destructor of ExamClass called!("<<val<<")"<<endl;
}
void Print()
{
cout<<val<<","<<member_obj1.GetX()<<","<<member_obj2.GetX()<<endl;
}
private:
MyClass member_obj1,member_obj2;
int val;
};
int main()
{
ExamClass obj1,obj2(1,2,3);
obj2.Print();
return 0;
}
错误:
c:\documents and settings\administrator\桌面\342\342.cpp(34) : error C2143: syntax error : missing ';' before 'private'
c:\documents and settings\administrator\桌面\342\342.cpp(38) : error C2143: syntax error : missing ';' before 'public'
c:\documents and settings\administrator\桌面\342\342.cpp(40) : error C2143: syntax error : missing ';' before '{'
c:\documents and settings\administrator\桌面\342\342.cpp(45) : error C2144: syntax error : missing ')' before type 'int'
程序是抄的原书上的, 因为两个类,不知道是哪边问题。。。 展开
#include<iostream.h>
class MyClass
{
public:
MyClass()
{
x = 0;
cout<<"Default constructor of MyClass called!"<<endl;
}
MyClass(int i)
{
x = i;
cout<<"Constructor of MyClass called!("<<x<<")"<<endl;
}
~MyClass()
{
cout<<"Destructor of MyClass called!("<<x<<")"<<endl;
}
int GetX()
{
return x;
}
void Print()
{
cout<<x<<endl;
}
private:
int x;
};
class ExamClass()
{
public:
ExamClass()
{
val = 0;
cout<<"Default constructor of ExamClass called!"<<endl;
}
ExamClass(int i, int j, int k):member_obj2(k),member_obj1(j)
{
val = i;
cout<<"Destructor of ExamClass called!("<<val<<")"<<endl;
}
~ExamClass()
{
cout<<"Destructor of ExamClass called!("<<val<<")"<<endl;
}
void Print()
{
cout<<val<<","<<member_obj1.GetX()<<","<<member_obj2.GetX()<<endl;
}
private:
MyClass member_obj1,member_obj2;
int val;
};
int main()
{
ExamClass obj1,obj2(1,2,3);
obj2.Print();
return 0;
}
错误:
c:\documents and settings\administrator\桌面\342\342.cpp(34) : error C2143: syntax error : missing ';' before 'private'
c:\documents and settings\administrator\桌面\342\342.cpp(38) : error C2143: syntax error : missing ';' before 'public'
c:\documents and settings\administrator\桌面\342\342.cpp(40) : error C2143: syntax error : missing ';' before '{'
c:\documents and settings\administrator\桌面\342\342.cpp(45) : error C2144: syntax error : missing ')' before type 'int'
程序是抄的原书上的, 因为两个类,不知道是哪边问题。。。 展开
1个回答
展开全部
class ExamClass() //这里多了括号了,去掉这个括号就行了
{
public:
....................
{
public:
....................
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询