2011-10-05 16 views
0

我不知道为什么我得到这个错误:list.cpp:127:error:'class List'has no member named'curr'错误:'class列表<RGBApixel>'没有任何成员,名为'curr'链接列表

template <class T> 
List<T> List<T>::mixSplit() 
{ 
    List<T> * newList; 
    class List<T>::ListNode * curr = head; 

    newList->length=0; 
    newList->length-3; 
    newList->head = NULL; 
    newList->tail=NULL; 

    for (int count=0;count<1;count++) 
     newList->curr=newList->curr->next; 

    newList->head=newList->curr; 
    newList->tail=tail; 

    tail=newList->curr->prev; 
    tail->next=NULL; 

    newList->head->prev=NULL; 
    newList->tail->next=NULL; 

    return newList; 
} 

错误是发生在

newList->curr=newList->curr->next; 

当我这样做:

class List<T>::ListNode * curr = head; 

不应该允许为curr成为newList的负责人?

编辑: 下面是类的定义:

template <class T> 
class List 
{ 
private: 
class ListNode 
{ 
    public: 
    // constructors; implemented in list_given.cpp 
    ListNode(); 
    ListNode(T const & ndata); 

    // member variables 
    ListNode * next; 
    ListNode * prev; 
    const T data; // const because we do not want to modify node's data 
}; 
+0

如何发布类List的定义? – littleadv

+0

你需要发布错误。从给定的代码,它看起来很好。 – iammilind

回答

0

使用typename的,而不是class

typename List<T>::ListNode * curr = head; //correct 
//class List<T>::ListNode * curr = head; //incorrect 

这是一个例子,你不能代替typename使用class

error: list.cpp:127: error: ‘class List’ has no member named ‘curr’

从错误中,很明显的是,模板的定义是.cpp文件。这是问题的另一个原因。您不能在另一个文件中提供模板的定义。声明和定义应该在同一个文件中。所以将所有的定义移动到头文件中。

+0

不起作用 – user949358

+0

@ user949358:这将起作用。否则,发布错误。 – Nawaz

+0

其相同的错误 – user949358

0

该类List没有会员curr,这就是错误说的。我很确定这个班确实没有那个成员。

事实上,在另一个具有相同名称的范围中有另一个变量 - 是无关紧要的。

0

问题是List<T>里面没有像curr这样的成员!您误解了,currList<T>::ListNode !!的成员。

只要在class之内声明class并不意味着所有的内部类成员都会委托给外部类,也就是说反之亦然。您需要在List<T>内部有ListNode* curr;来完成此操作。

解决编译器错误后,您将最终解决运行时错误,因为您的类还有其他几个问题。例如,

List<T> * newList; // dangling/uninitialized pointer 
class List<T>::ListNode * curr = head; // curr is unused and pointing to something unknown 

您必须初始化newList = new List<T>;以使用指针。或者你可以声明一个自动对象为List<T> newList;