0

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!

4
  • Why are you making your own linked list in the first place? C++ already has std::list. Commented Dec 12, 2011 at 14:14
  • @sbi: I have a precognition: "Learning purposes". Commented Dec 12, 2011 at 14:16
  • @sbi: I am doing my own list because I wasn't aware of the fact C++ already has them. Thank you for your hint. Commented Dec 12, 2011 at 14:22
  • @drakide: Yes! C++ has lots of tools built-in! I'll recommend you a good introductory C++ book. Commented Dec 12, 2011 at 14:28

2 Answers 2

8

Pointers are accessed with ->:

cur_node = cur_node->follower;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! This solved my problem. Pointers are still pretty confusing to me.
@drakide: If this solved your problem, please click on the little tick right beside the answer to mark this one as "accepted". Thanks!
5
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.