1

因此,我一直在玩 Nodes,并在尝试对其进行测试时不断遇到此错误。如果我使用括号,我会收到此错误list.- “表达式必须具有类类型!”

如果我不使用括号,我会得到这个错误listinsert并且display- “这是无法访问的。”

在 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
4

1 回答 1

1

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

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

线索在错误“这是无法访问的”中。因为您没有给出任何访问修饰符,所以这个类的所有成员都默认为私有。要解决此问题,您只需标记班级的公共和私人部分:

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

    private:
        Node<TYPE>* front;
};

通过此更改,您的代码应该可以在变量声明末尾使用或不使用括号list

于 2012-09-24T01:14:52.127 回答