我收到错误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();
}
答案 0 :(得分:33)
成员函数/构造函数/析构函数的类内定义不需要student::
等资格。
所以这段代码,
student::student()
{
id=0;
strcpy(name,"undefine");
}
应该是这样的:
student()
{
id=0;
strcpy(name,"undefine");
}
只有在类外定义成员函数时才需要限定student::
,通常在.cpp文件中。
答案 1 :(得分:2)
如果构造函数的定义出现在类定义之外,那将是正确的。
答案 2 :(得分:0)
类似的代码:
更改自:
class Solution {
public:
static int Solution::curr_h; // ----------------- ISSUE: "Solution::" is extra
static int Solution::curr_m; // ----------------- ISSUE: "Solution::" is extra
};
int Solution::curr_h = 0;
int Solution::curr_m = 0;
收件人:
class Solution {
public:
static int curr_h; // ----------------- FIX: remove "Solution::"
static int curr_m; // ----------------- FIX: remove "Solution::"
};
int Solution::curr_h = 0; // <------ good here - "Solution::" is required
int Solution::curr_m = 0; // <------ good here - "Solution::" is required
static
时需要在类外进行初始化,但同时也需要所有声明(int
)。