C++链表奇怪的编译错误
问题描述:
我试图创建在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位计算机上
谢谢!
答
node *cur_node = &m_list;
cur_node
是node
for (int count=0; count<index; count++)
{
cur_node = cur_node->follower;
}
指针应工作
为什么你在第一时间使自己的链接列表? C++已经有'std :: list'。 – sbi
@sbi:我猜是任务。 –
@sbi:我有预知:“学习目的”。 – Xeo