2012-09-24 536 views
1

所以我一直在玩节点,并在尝试测试时一直遇到这个错误。如果我使用圆括号,则在list.上得到此错误 - “表达式必须具有类类型!”表达式必须具有类类型?

如果我不使用小括号,我在list,insertdisplay上得到这个错误 - “这是无法访问的。”

在Main()中声明我的LList时会出现这种情况。发生了什么,为什么是这样?

我的司机

#include "LList.h" 
#include <iostream> 
using namespace std; 

int main() 
{ 
    LList<int> list; 
    bool test = list.insert(5); 
    list.display(); 

    return 0; 
} 

类LLIST

#include "Nodes.h" 
#ifndef LLIST_H 
#define LLIST_H 

template<typename TYPE> 
class LList 
{ 
    Node<TYPE>* front; 
    LList(); 
    ~LList(); 
    bool insert(const TYPE& dataIn); 
    void display() const; 
}; 

template<typename TYPE> 
LList<TYPE>::LList() 
{ 
    front = null; 
}; 

template<typename TYPE> 
LList<TYPE>::~LList() 
{ 
    Node<TYPE>* temp; 
    while(front) 
    { 
     temp = front; 
     front = fornt -> next; 
     delete temp; 
    } 
}; 

template<typename TYPE> 
bool LList<TYPE>::insert(const TYPE& dataIn) 
{ 
    bool success = false; 
    Node<TYPE> pBefore = null; 
    Node<TYPE> pAfter = front; 

    while(pAfter && PAfter->data < dataIn) 
    { 
     pBefore = pAfter; 
     pAfter = pAfter->next; 
    } 

    if(Node<TYPE>* store = new Node<TYPE>) 
     store->data = dataIn 

    return success; 
}; 

template<typename TYPE> 
void LList<TYPE>::display() const 
{ 
    TYPE* temp = front; 
    while(front && temp->next != null) 
    { 
     cout << temp->data << endl; 
    } 
}; 

#endif 

类节点

#ifndef NODES_H 
#define NODES_H 

template<typename TYPE> 
struct Node 
{ 
    Node<TYPE>* next; 
    TYPE data; 
    Node(); 
    Node(TYPE d, Node<TYPE> n); 
}; 
template<typename TYPE> 
Node<TYPE>::Node() 
{ 
    data = 0; 
    next = null; 
}; 
template<typename TYPE> 
Node<TYPE>::Node(TYPE d, Node<TYPE> n) 
{ 
    data = d; 
    next = n; 
}; 

#endif 
+0

“如果我使用小括号”......在哪里? –

+0

我得到错误''列表;''和''列表();''; –

回答

1

你的错误是你的类声明的结果:

template<typename TYPE> 
class LList 
{ 
    Node<TYPE>* front; 
    LList(); 
    ~LList(); 
    bool insert(const TYPE& dataIn); 
    void display() const; 
}; 

线索出现错误“This is inaccesible。”因为您没有提供任何访问修饰符,所以此类的所有成员都默认为私有。为了解决这个问题,你只需要标注类的公共和私人部分:

template<typename TYPE> 
class LList 
{ 
    public: 
     LList(); 
     ~LList(); 
     bool insert(const TYPE& dataIn); 
     void display() const; 

    private: 
     Node<TYPE>* front; 
}; 

随着这一变化,您的代码应该有或没有为list您的变量声明的末尾括号工作。

相关问题