2013-02-19 140 views
0

與C++基礎相關。 我正在創建一個單獨鏈接列表。如何定義指向類成員函數的指針

class Linked_List 
{ 
public: Linked_List(); 
    ~Linked_List(); 
     //Member Functions 
    struct node* getHead(void); 
private: 
    struct node{ 
     int d; 
     struct node* next; 
    }*head; 
}; 
struct node (Linked_List::*getHead)(void) 
{ 
    return head; 
} 

我收到此錯誤:

"error C2470: 'getHead' : looks like a function definition, but there is no parameter list; skipping apparent body".

我試着在谷歌搜索,但沒有用的。任何建議plz。

回答

5

你並不需要一個成員函數指針,你只是想爲函數提供定義

Linked_List::node* Linked_List::getHead() 
{ 
    return head; 
} 

另請注意,該struct關鍵字在函數定義是不必要的,而您必須限定結構node的名稱,並在其定義的範圍內使用該類的名稱。

此外,void關鍵字指定一個空的參數列表是不必要的。因此,我建議你改寫類定義如下:

class Linked_List 
{ 
private: 
    struct node 
    { 
     int d; 
     struct node* next; 
    }; 
    node *head; 
public: 
    Linked_List(); 
    ~Linked_List(); 
    node* getHead(); 
}; 
+0

什麼是'int d;'用於? – bash0r 2013-02-19 23:30:00

+0

@ bash0r:不知道。詢問OP :-) – 2013-02-19 23:30:34

+0

啊,沒有看到他有... – bash0r 2013-02-19 23:31:15