我收到一个错误extraqualification 'student::' on member 'student' [-fpermissive]
。
还有为什么 name::name
这样的语法会用在构造函数中?
#include<iostream>
#include<string.h>
using namespace std;
class student
{
private:
int id;
char name[30];
public:
/* void read()
{
cout<<"enter id"<<endl;
cin>>id;
cout<<"enter name"<<endl;
cin>>name;
}*/
void show()
{
cout<<id<<name<<endl;
}
student::student()
{
id=0;
strcpy(name,"undefine");
}
};
main()
{
student s1;
// s1.read();
cout<<"showing data of s1"<<endl;
s1.show();
// s2.read();
//cout<<"showing data of s2"<<endl;
//s2.show();
}
最佳答案
成员函数/构造函数/析构函数的类内定义不需要诸如 student::
之类的限定条件。
所以这段代码,
student::student()
{
id=0;
strcpy(name,"undefine");
}
应该是这样的:
student()
{
id=0;
strcpy(name,"undefine");
}
student::
仅当您在类外定义成员函数时才需要,通常在 .cpp 文件中。
关于c++ - 成员 "extra qualification ‘student::’ [-fpermissive] 上的错误 ‘student’ “,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11692806/