C++ 错误“额外资格 'student::' 对成员 'student' [-fpermissive] ”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11692806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error "extra qualification ‘student::’ on member ‘student’ [-fpermissive] "
提问by wizneel
I am getting an error extra qualification ‘student::' on member ‘student' [-fpermissive]
.
And also why name::name
such syntax is used in constructor?
我收到一个错误extra qualification ‘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();
}
回答by Nawaz
In-class definitions of member function(s)/constructor(s)/destructor don't require qualification such as student::
.
成员函数/构造函数/析构函数的类内定义不需要限定,例如student::
.
So this code,
所以这段代码,
student::student()
{
id=0;
strcpy(name,"undefine");
}
should be this:
应该是这样的:
student()
{
id=0;
strcpy(name,"undefine");
}
The qualification student::
is required only if you define the member functions outside the class, usually in .cpp file.
资格student::
,如果你定义类的外部成员函数只需要,通常在.cpp文件。
回答by tumdum
It would be correct if definition of constructor would appear outside of class definition.
如果构造函数的定义出现在类定义之外,那将是正确的。