繁体   English   中英

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

[英]c++ linked list strange compiler-error

我正在尝试使用以下代码在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 ::节点*'

但是,“跟随者”似乎几乎是一个节点!

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

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

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

应该管用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM