c++ - 成员 "extra qualification ‘student::’ [-fpermissive] 上的错误 ‘student’ “

标签 c++ constructor

我收到一个错误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/

相关文章:

c++ - 将函数指针reinterpret_cast与void(*)()比较是否为未定义行为?

c++ - 运行时错误 : reference binding to misaligned address 0xbebebebebebebec6 for type 'int' ,,需要 4 字节对齐 (STL_vector.h)

java - 为什么需要一个无参数的构造函数来运行这个简单的 Spring 配置?

c++ - 使用析构函数后,代码显示 "reference to Book::~Book()"错误

c# - 如何继承构造函数?

C++链表——析构函数实现

c++ - wstring null 终止了吗?

c++ - 通过使用其他 namespace 元素传播的 namespace 外部的可见性

c++ - 为什么在没有返回类型的情况下显式调用构造函数时会返回一个临时对象?

java - 为每个创建的类调用构造函数