C++类出现了E:\434\main.cpp|25|error: 'as' was not declared in this scope|,该如何解决
C++类出现了E:\434\main.cpp|25|error: 'as' was not declared in this scope|
#include <iostream>
using namespace std;
class a{
public :
void as();
void as1();
};
void a::as()
{
cout<<"1233455"<<endl;
};
void a::as1()
{
cout<<"$#$32#$@#"<<endl ;
};
int main()
{
cout <<"shuri"<<endl ;
int a;
cin>>a;
if(a==1)
as();
else
as1();
cout << "Hello world!" << endl;
return 0;
}
------解决思路----------------------
as 和 as1 是类 a 的成员函数,不能直接调用,需要定义一个类 a 的对象去调用这两个函数。
你可以这样写:
#include <iostream>
using namespace std;
class a{
public :
void as();
void as1();
};
void a::as()
{
cout<<"1233455"<<endl;
};
void a::as1()
{
cout<<"$#$32#$@#"<<endl ;
};
int main()
{
cout <<"shuri"<<endl ;
int a;
cin>>a;
if(a==1)
as();
else
as1();
cout << "Hello world!" << endl;
return 0;
}
------解决思路----------------------
as 和 as1 是类 a 的成员函数,不能直接调用,需要定义一个类 a 的对象去调用这两个函数。
你可以这样写:
int main()
{
a a1;
cout <<"shuri"<<endl ;
int i;
cin>>i;
if(i==1)
as();
else
as1();
cout << "Hello world!" << endl;
return 0;
}