0

我正在尝试定义一个向量<Queue>。这个向量应该包含每个队列对象的起始地址。我在 List.h 中创建了一个复制构造函数和一个赋值运算符(队列有私有:列表)。但是,即使在我的程序运行之前,它也会引发错误: List.h:156:1: error: invalid use of template-name 'List' without a argument 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();
}
}

文件队列.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 
4

1 回答 1

0

您缺少返回值的模板参数

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

你需要那个,因为编译器不知道他正在解析一个List成员,这List意味着当前模板实例化,直到 after List<NODETYPE>::

于 2012-04-21T12:29:04.293 回答