错误:'class List <rgbapixel>'没有名为'curr'的成员链接列表</rgbapixel>

时间:2011-10-05 09:21:11

标签: c++ linked-list

我不知道为什么我收到此错误:list.cpp:127:错误:'class List'没有名为'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
};

3 个答案:

答案 0 :(得分:0)

使用typename代替class

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

这是一个例子,您无法使用class代替typename

  

错误: list.cpp :127:错误:'class List'没有名为'curr'的成员

从错误中可以清楚地看到模板的定义在.cpp文件中。这是问题的另一个原因。您无法在另一个文件中提供模板的定义。声明和定义应该在同一个文件中。所以将所有定义移到头文件中。

答案 1 :(得分:0)

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

另一个具有相同名称的范围内的另一个变量这一事实无关紧要。

答案 2 :(得分:0)

问题是curr内没有List<T>这样的成员!你误解了,currList<T>::ListNode !!

的成员

class内声明class并不意味着所有内部类成员也被委托给外部类,反之亦然。您需要在ListNode* curr;List<T>才能完成此操作。

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

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;

相关问题