drakide
drakide

Reputation: 1757

c++ linked list strange compiler-error

I am trying to create a linked list in C++ using the following Code

int main ()
{
    return 0;
}

class LList
{
private:
    struct node
    {
        int value;
        node *follower;  // node definitely is a node
    };

    node m_list;
    int m_length;
public:
    LList ();
    void insert (int index, int value);
    int get_length () const { return m_length; }
};

LList::LList ()
{
    m_length = 0;
    m_list.follower = 0;
}
void LList::insert (int index, int value)
{
    node *cur_node = &m_list;
    for (int count=0; count<index; count++)
    {
        cur_node = cur_node.follower;  // << this line fails
    }
}

(This is not my original code, so please ignore any unlogic things, bad naming...)

Compiling it with g++ results in the following compiler error

main.cpp: In member function ‘void LList::insert(int, int)’: main.cpp:33:29: error: request for member ‘follower’ in ‘cur_node’, which is of non-class type ‘LList::node*’

However 'follower' pretty much seems to be a node!?

Notes: -I am using g++ 4.6.2 using the command

g++ main.cpp -Wall -g -o my_program

-Working on a fedora 16 64Bit machine

Thanks in advance!

Upvotes: 0

Views: 141

Answers (2)

KoKuToru
KoKuToru

Reputation: 4115

node *cur_node = &m_list;

cur_node is pointer of node

for (int count=0; count<index; count++)
{
    cur_node = cur_node->follower; 
}

should work

Upvotes: 5

Xeo
Xeo

Reputation: 131887

Pointers are accessed with ->:

cur_node = cur_node->follower;

Upvotes: 8

Related Questions