C ++链表奇怪的编译器错误

c++ linked list strange compiler-error

本文关键字:编译器 错误 链表      更新时间:2023-10-16

我正在尝试使用以下代码在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(' 中: 主.cpp:33:29:错误:在"cur_node"中请求成员"关注者", 非类类型为"LList::node*">

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

笔记:-我正在使用命令使用 g++ 4.6.2

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

-在软呢帽 16 64 位机器上工作

提前感谢!

指针可通过->访问:

cur_node = cur_node->follower;
node *cur_node = &m_list;

cur_nodenode指针

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

应该工作