c ++链表怪编译错误

时间:2011-12-12 14:12:04

标签: c++ linked-list

我正在尝试使用以下代码

在C ++中创建链接列表
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
    }
}

(这不是我的原始代码,所以请忽略任何不逻辑的东西,错误的命名......)

使用g ++编译会导致以下编译器错误

  

main.cpp:在成员函数'void LList :: insert(int,int)'中:   main.cpp:33:29:错误:请求'cur_node'中的成员'follower',   这是非类型'LList :: node *'

然而,“追随者”似乎是一个节点!?

注意: - 我正在使用g ++ 4.6.2使用命令

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

- 在fedora 16 64Bit机器上工作

提前致谢!

2 个答案:

答案 0 :(得分:7)

使用->

访问指针
cur_node = cur_node->follower;

答案 1 :(得分:4)

node *cur_node = &m_list;

cur_nodenode

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

应该有效