2011-12-12 97 views
0

我试图创建在C链表++使用下面的代码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 
    } 
} 

(这不是我的原代码,所以请忽略任何unlogic的东西,不好的命名.. 。)

使用g ++编译的结果在它下面的编译器错误

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*’

然而“跟随”几乎似乎是一个节点!?

注: -I正在使用克++ 4.6.2使用命令

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

预先 - 工作在Fedora 16 64位计算机上

谢谢!

+0

为什么你在第一时间使自己的链接列表? C++已经有'std :: list'。 – sbi

+0

@sbi:我猜是任务。 –

+0

@sbi:我有预知:“学习目的”。 – Xeo

回答

7

指针与->访问:

cur_node = cur_node->follower; 
+0

谢谢!这解决了我的问题。指针仍然让我感到困惑。 – drakide

+1

@drakide:如果这解决了您的问题,请点击答案旁边的小勾,将其标记为“已接受”。谢谢! – Xeo

4
node *cur_node = &m_list; 

cur_nodenode

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

指针应工作