2012-07-27 137 views
12

我收到錯誤extra qualification ‘student::’ on member ‘student’ [-fpermissive]
而且爲什麼name::name這樣的語法在構造函數中使用?錯誤「額外資格」學生::'在成員'學生'[-fpermissive]「

#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(); 
} 

回答

30

在-類的成員函數(S)/構造(多個)的定義/析構函數不需要資格如student::

所以這個代碼,

student::student() 
{ 
    id=0; 
    strcpy(name,"undefine"); 
} 

應該是這樣的:

student() 
{ 
    id=0; 
    strcpy(name,"undefine"); 
} 

如果你定義的成員函數的類外,一般在.cpp文件中的資格student::時,才需要。

+0

什麼是這種資格的使用或當我們必須使用它 – wizneel 2012-07-27 17:40:45

+2

@ user1306088:資格'學生::'只有在您定義函數以外的類時才需要,通常在.cpp文件中。 – Nawaz 2012-07-27 17:41:10

2

如果構造函數的定義會出現在類定義之外,那將是正確的。