Sumanth
Sumanth

Reputation: 605

How to define a pointer to a class member function

Related to C++ Basics. I am creating a singly linked list.

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;
}

I am getting this error:

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

I tried to search in google but of no use. Any suggestions plz.

Upvotes: 0

Views: 234

Answers (1)

Andy Prowl
Andy Prowl

Reputation: 126552

You don't need a pointer to member function, you just want to provide a definition for that function:

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

Also notice, that the struct keyword is unnecessary in the function definition, while you have to qualify the name of the struct node with the name of the class in the scope of which it is defined.

Also, the void keyword to specify an empty argument list is unnecessary. I therefore suggest you to rewrite the class definition as follows:

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

Upvotes: 5

Related Questions