尝试使此构造函数通用时,为什么会出现编译错误?

时间:2020-03-18 21:50:51

标签: c++ list generics linked-list queue

所以我做了这三个课程

template <class dataType>
class listEntry
{

      private:
              dataType data;
      public:
              listEntry *next;
              listEntry *prev;

              dataType getData() { return this->data; }

              listEntry();
              listEntry(dataType data) { this->data = data; }
              ~listEntry() {};    

};

template <class dataType>
class List
{

      private:
              dataType *head;
              dataType *tail;
              int count;
      public:
             dataType *getHead() { return this->head; }
             dataType *getTail() { return this->tail; }

             void addToTail(dataType *newEntry);
             void addToHead(dataType *newEntry);

             int getCount() { return count; }

             void printListForward();

             List();
             List(const List<dataType> &li);
             ~List();  

};

template <class dataType>
class Queue: public List<dataType>
{

      public:
             void enQueue(dataType *newEntry);

             Queue():List<dataType>() { return; }
             Queue(const List<dataType>& li):List<dataType>(li) { return; }
             Queue(dataType data);
             Queue(listEntry<dataType> le);

}

我的Queue构造函数之一是:

template <class dataType>
Queue<dataType>::Queue(dataType data)
{

  enQueue(new listEntry<int>(data));                               

}

但是,我希望enQueue(new listEntry<int>(data));enQueue(new listEntry<dataType>(data));,以便它是通用的并且可以与任何数据类型一起使用。但是当我将其更改为该格式时,会出现编译错误。

enQueue定义如下:

template <class dataType>
void Queue<dataType>::enQueue(dataType *newEntry)
{

     this->addToTail(newEntry);     

}

为什么写dataType而不是int会出现编译错误?

1 个答案:

答案 0 :(得分:0)

您的代码存在一些设计问题:

  • head的{​​{1}}和tail成员需要声明为List,而不是listEntry<dataType>*

  • dataType*addToTail()addToHead()应该全部使用enQueue()值而不是dataType指针。

  • dataType*addToTail()需要在内部创建一个新的addToHead()对象。

尝试更多类似方法:

listEntry<dataType>

然后您可以执行以下操作:

template <class dataType>
class listEntry
{
private:
    dataType data;
public:
    listEntry *prev;
    listEntry *next;

    dataType getData() const { return data; }

    listEntry(const dataType &data, listEntry *prev = NULL, listEntry *next = NULL);
};

template <class dataType>
class List
{
public:
    typedef listEntry<dataType> entryType;
private:
    entryType *head;
    entryType *tail;
    int count;
public:
    entryType* getHead() { return head; }
    const entryType* getHead() const { return head; }

    entryType* getTail() { return tail; }
    const entryType* getTail() const { return tail; }

    void addToTail(const dataType &data);
    void addToHead(const dataType &data);

    int getCount() const { return count; }

    void printListForward() const;

    List();
    List(const List &li);
    ~List();  

    List& operator=(const List &li);
};

template <class dataType>
class Queue : public List<dataType>
{
public:
    typedef List<dataType> listType;

    void enQueue(const dataType &data);

    Queue() {}
    Queue(const Queue &q) : listType(q) {}
    Queue(const listType &li) : listType(li) {}
    Queue(const dataType &data);
};

...

template<class dataType>
listEntry<dataType>::listEntry(const dataType &data, listEntry *prev, listEntry *next)
    : data(data), prev(prev), next(next)
{
}

template<class dataType>
void List<dataType>::addToTail(const dataType &data)
{
    entryType *newEntry = new entryType(data, tail);
    if (!head) head = newEntry;
    if (tail) tail->next = newEntry;
    tail = newEntry;
    ++count;
}

template<class dataType>
void List<dataType>::addToHead(const dataType &data)
{
    entryType *newEntry = new entryType(data, NULL, head);
    if (!tail) tail = newEntry;
    if (head) head->prev = newEntry;
    head = newEntry;
    ++count;
}

template<class dataType>
void List<dataType>::printListForward() const
{
    entryType *entry = head;
    while (entry)
    {
        //...
        entry = entry->next;
    }
}

template<class dataType>
void List<dataType>::List()
    : head(NULL), tail(NULL), count(0)
{
}

template<class dataType>
void List<dataType>::List(const List<dataType> &li)
    : head(NULL), tail(NULL), count(0)
{
    const entryType *entry = li.getHead();
    while (entry)
    {
        addToTail(entry->getData());
        entry = entry->next;
    }
}

template<class dataType>
void List<dataType>::~List()
{
    entryType *entry = head;
    while (entry)
    {
        entryType *next = entry->next;
        delete entry;
        entry = next;
    }
}

template<class dataType>
List<dataType>& List<dataType>::operator=(const List<dataType> &li)
{
    if (&li != this)
    {
        List<dataType> temp(li);
        std::swap(head, temp.head);
        std::swap(tail, temp.tail);
        std::swap(count, temp.count);
    }
    return *this;
}

template <class dataType>
Queue<dataType>::Queue(const dataType &data)
{
    enQueue(data);
}

template <class dataType>
void Queue<dataType>::enQueue(const dataType &data)
{
    addToTail(data);
}