用户定义的“队列”类的向量

时间:2012-04-21 12:22:27

标签: c++ vector constructor overloading operator-keyword

我正在尝试定义一个矢量<队列>。此向量应包含每个队列对象的起始地址。我在List.h中创建了一个复制构造函数和赋值运算符(queue has private:list)。但是,即使在我的程序运行之前,它也会引发错误:List.h:156:1:错误:无法使用模板名称'List'而没有参数列表

这是代码(箭头指出相关位)

#ifndef LIST_H
#define LIST_H
#include "ListNode.h"

using namespace std;
template <typename NODETYPE>
class List {
public:
    List(const List&);                    <-----------copy constructor 
    List();
    ~List();
    void insertAtFront(const NODETYPE& );
    void insertAtBack (const NODETYPE& );
    bool removeFromFront(NODETYPE& );
    bool removeFromBack (NODETYPE& );
    bool isEmpty() const;
    void print() const;
    int length() const;
    List<NODETYPE>& operator=(const List&);     <----- assignment operator overload (see end)
private:
    ListNode<NODETYPE> *firstPtr;
    ListNode<NODETYPE> *lastPtr;
    ListNode<NODETYPE> *getNewNode (const NODETYPE& );
};

template <typename NODETYPE>                <------------code for copy constructor
List<NODETYPE>::List(const List& source){
    firstPtr = source.firstPtr;
    lastPtr = source.lastPtr;}


template <typename NODETYPE>
List<NODETYPE>::List():firstPtr(0),lastPtr(0){}

template <typename NODETYPE>
List<NODETYPE>::~List(){
    if (!isEmpty()){
        cout<<"\nDestroying node ... \n";
        ListNode<NODETYPE> *currentPtr = firstPtr;
        ListNode<NODETYPE> *tmpPtr;
        while (currentPtr != 0){
            tmpPtr = currentPtr;
            cout<<tmpPtr->value<<endl;
            currentPtr = currentPtr->nextPtr;
            delete tmpPtr;
        }
        }
    cout<<"\nAll node destroyed\n";
    }

template <typename NODETYPE>
void List<NODETYPE>::insertAtFront(const NODETYPE &val){
    ListNode<NODETYPE>*newPtr = getNewNode(val);
    if (isEmpty()){
        firstPtr = lastPtr = newPtr;}
    else {
        newPtr ->nextPtr = firstPtr;
        firstPtr = newPtr;                                          // do not understand this frag
    }}

template <typename NODETYPE>
void List<NODETYPE>::insertAtBack(const NODETYPE& val){
        ListNode<NODETYPE>*newPtr = getNewNode(val);
    if (isEmpty()){
        firstPtr = lastPtr = newPtr;}
    else {
        lastPtr ->nextPtr =newPtr;
        lastPtr = newPtr;                                           // do not understand this frag
    }

}

template <typename NODETYPE>
bool List<NODETYPE>::removeFromFront(NODETYPE& data)
{
    if (isEmpty()){
        return false;}
    else
    {
        ListNode<NODETYPE> *tempPtr = firstPtr;
        if (firstPtr == lastPtr)
            firstPtr = lastPtr = 0;
        else
            firstPtr = firstPtr->nextPtr;           // must look at detail inside DDD, what happens to the current ListNode object pointed to byfirstPtr
        data = tempPtr->value;
        delete tempPtr;
        return true;
    }
}
template <typename NODETYPE>
bool List<NODETYPE>::removeFromBack (NODETYPE& data) {

    if (isEmpty){
        return false;}
    else
    {
        ListNode<NODETYPE> *tempPtr = firstPtr;
        if (firstPtr == lastPtr)
            firstPtr = lastPtr = 0;
        else
        {
            ListNode<NODETYPE> *currentPtr = firstPtr;
            while (currentPtr->nextPtr !=lastPtr){
                currentPtr = currentPtr->nextPtr; }
            lastPtr = currentPtr;
            currentPtr ->nextPtr = 0;
        }
        data= tempPtr->value;
        delete tempPtr;
        return true;
    }
    }

template <typename NODETYPE>
bool List<NODETYPE>::isEmpty() const{                               // CONSTANT MEMBER FUNCTION
    return firstPtr==0;
    }

                                                                // different from textbook (see p 755)
template <typename NODETYPE>
ListNode<NODETYPE>* List<NODETYPE>::getNewNode(const NODETYPE&value) {
    return new ListNode <NODETYPE>(value); }



template<typename NODETYPE>
void List<NODETYPE>::print() const {
    if (isEmpty()) {
        cout<<" This list is empty\n";
        return ;}
    ListNode<NODETYPE> *currentPtr = firstPtr;
    while (currentPtr !=0)
    {
        cout << currentPtr->value <<' ';
        currentPtr = currentPtr->nextPtr;
        }
    cout<< "\n\n";

}

template <typename NODETYPE>
int List<NODETYPE>::length() const {
    if (this->isEmpty()){
        cout<< "\n Return size of what ? .. this list is empty ";
        return 0; }
    int size = 1;
    ListNode<NODETYPE>* currentPtr = firstPtr;
    while (currentPtr != lastPtr) {
        currentPtr = currentPtr->nextPtr;
        size +=1;
        }
    return size;
}

 template<typename NODETYPE>                       
List<NODETYPE>& List<NODETYPE>::operator=(List& rhs)       <---- line of error
{
    if (this !=&rhs){
        this->firstPtr = rhs.firstPtr;
        this->lastPtr = rhs.lastPtr; 
        return &firstPtr;
    }

   #endif

调用程序如下:

#include <iostream>List& operator=(const List&);
#include <vector>
#include "Queue.h"
#include <algorithm>
using namespace std;

int main () {
    vector < Queue <int> > dodo;
    for (int init = 0; init<4;init++ ){
        dodo.push_back(init);
    vector <Queue <int> >::const_iterator iterator;
    for (iterator = dodo.begin();iterator !=dodo.end();iterator ++){
        cout<<"Address of each queue item "<<dodo[]<<'\t'<<&dodo[];}
    int dequeueInteger;
    Queue <int> intQueue;
    cout <<"\n Size is .. " <<intQueue.howbig();
    cout << "\nProcessing integer Queue\n";
    for (int i=0;i<3;++i) {
        intQueue.enqueue(i);
        intQueue.printQueue(); }
    Queue<int> photo;
    photo = intQueue;

    cout <<"\n\n Here is mr. photo";
    photo.printQueue();

cout<<endl<<"\nSize of this queue is .. " <<intQueue.howbig();
cout<<endl;
while (!intQueue.isQueueEmpty()){
    intQueue.dequeue(dequeueInteger);
    cout<<dequeueInteger<<" dequeued\n";
    intQueue.printQueue();
}
}

文件Queue.h

    #ifndef QUEUE_H
#define QUEUE_H
#include "List.h"

template <typename QUEUETYPE>
class Queue : private List< QUEUETYPE >
{
public:
void enqueue (const QUEUETYPE &data) {
    insertAtBack(data);}
bool dequeue (QUEUETYPE &data) {
    return removeFromFront(data); }
bool isQueueEmpty ()const {
    return this->isEmpty(); }
void printQueue() const {
    this ->print(); }
int howbig() const {
    return this->length();
    }


};
   #endif 

1 个答案:

答案 0 :(得分:0)

您缺少返回值的模板参数

template<typename NODETYPE>                       
List<NODETYPE>& List<NODTETYPE>::operator=(List& rhs)
    ^^^^^^^^^^

您需要这样做,因为编译器不知道他正在解析List成员,其中List表示当前模板实例化,直到List<NODETYPE>::之后。