简体   繁体   English

编码队列模板类时出现错误C2955

[英]Error C2955 while coding a Queue template class

Making a queue class using templates for my data structures class. 使用我的数据结构类的模板制作队列类。 I'm getting a C2955 "Queue - use of class template requires template argument list" when I build, among a bunch of other errors which I think spawn from this one. 在构建时,我收到一个C2955“队列-使用类模板需要模板参数列表”,以及我认为由此产生的许多其他错误。 I've compared this program to others which used templates and I can't seem to figure out what's different about this. 我已经将此程序与其他使用模板的程序进行了比较,但似乎无法弄清楚此程序有何不同。 Can anyone help me through it? 谁能帮我解决这个问题?

//Queue.cpp
#include <iostream>
#include "Queue.h"

using namespace std;

//Default constructor for Queue object
template<class ItemType>
Queue<ItemType>::Queue() {
    front = 0;
    back = 0;
    count = 0;
}

//Check if the queue is empty
template<class ItemType>
bool Queue<ItemType>::empty() const {

    if (count == 0) {
        return true;
    }
    else {
        return false;
    }
}

//Remove the first item in the queue
template<class ItemType>
bool Queue<ItemType>::dequeue() {

    int p = count;

    for (int i = 0; i < count - 1; i++) {
        items[i] = items[i + 1];
    }

    count--;

    if (p > count) {
        return true;
    } else {
        return false
    }

}

//Add an entry to the beginning of the queue
template<class ItemType>
bool Queue<ItemType>::enqueue(const itemType &item) { 

    count++;
    items[count - 1] = item;

    return true;
}

template<class ItemType>
bool Queue<ItemType>::peekFront(itemType &item) const {
    item = items[0];
    return true;
}

template<class ItemType>
int Queue<ItemType>::getSize() const {
    return count;
}  

There are no apparent syntax errors in Queue.cpp that I can find. 我发现Queue.cpp中没有明显的语法错误。 I know that back and front aren't used but I'm not sure what they're meant for yet. 我知道前面和后面都没有用,但是我不确定它们的含义是什么。 Our professor provided us with most of the header file, and the main. 我们的教授为我们提供了大多数头文件和主要文件。

//Queue.h
#ifndef _QUEUE
#define _QUEUE
#include<iostream>
#include<vector>
using namespace std;
const int MAX_SIZE=10;
typedef int itemType;

template <class ItemType>
class Queue {
public:
    Queue();  // Constructor. Initialize front=0, back=0, count=0 
    bool empty() const; // To test if the queue is empty. Return true if it          is, flase if it is not. 
    bool dequeue(); // Remove the front entry from the queue
    bool enqueue(const itemType &item); // Add new entry called item at the     back of the queue. 
    bool peekFront(itemType &item) const; // Retrieve the front entry from the queue
    int getSize() const ;  // To get the number of the entries in the queue


    vector<itemType> toVector() const  // to convert the queue to a vector
    {
        vector<itemType> vectorQ;

        int i=front;
        int size=count;
        while (size>0)
        {
            i=i%MAX_SIZE;
            vectorQ.push_back(items[i]);
            i++;
            size--;
        }
        return vectorQ;

    }

private:
    int front, back; 
    int count;
    itemType items[MAX_SIZE];  // items is a circular array to store the queue.

};

#endif

In the main, when "Queue q;" 在主要情况下,当“队列q;”时 is called to make an empty queue object, there is a red line saying "argument list for class template "Queue" is missing 调用以创建一个空队列对象,红线显示“缺少类模板“队列”的参数列表”

#include "Queue.h"
#include "Queue.cpp"

using namespace std;

void displayQ(Queue & queue)
{
    cout << "The queue contains :\n" ;
    vector<int> queueItems=queue.toVector();
    for (int i=0; i<queue.getSize(); i++)
    {
        cout <<queueItems[i] << " ";
    }
    cout << endl;
}    

int main()
{
    Queue q;  //create an empty queue

    bool flag=q.empty(); // To test if the queue is really empty.
    if (flag)  
        cout <<"The queue is empty.\n";

    q.enqueue(1);  //To test the enqueue function by inserting a set of numbers (1-10) into q. 
    q.enqueue(2);
    q.enqueue(3);
    q.enqueue(4);
    q.enqueue(5);
    q.enqueue(6);
    q.enqueue(7);
    q.enqueue(8);
    q.enqueue(9);
    q.enqueue(10);
    displayQ(q);  // Display the contents in q.

    int buffer;   // To test the peekFront function. The buffer should hold the the value of the front entry  
    q.peekFront(buffer);
    cout << "The front entry of the queue is " << buffer << " .\n";

    flag=q.enqueue(11); // To test the returned value of the enqueue fnction. It returns flase when the q has no room 
    if (!flag)
        cout << "The queue is full. No room for insertion.\n";

    q.dequeue(); // To test the dequeue function. Remove the first two entries from the q.
    q.dequeue();
    displayQ(q);  // Display the contents in q.

    q.enqueue(11);  // Does the q have room to hold two more new entry?
    q.enqueue(12);
    displayQ(q);  // Display the contents in q.

    q.peekFront(buffer); // what's the front entry of the q now?  
    cout << "The front entry of the queue is " << buffer << " .\n";

}

Other errors I get when building are C2662 and C2244 我在构建时遇到的其他错误是C2662和C2244

As the error message says, you have to add template argumet list to use Queue . 如错误消息所述,您必须添加模板argumet列表才能使用Queue

Change 更改

void displayQ(Queue & queue)

to

template<class itemType>
void displayQ(Queue<itemType> & queue)

Then, change 然后,改变

Queue q;  //create an empty queue

in main() to main()

Queue<int> q;  //create an empty queue

Finally, change 最后,改变

return false

in bool Queue<ItemType>::dequeue() to bool Queue<ItemType>::dequeue()

return false;

(add a semicolon) (添加分号)

Also you should remove #include "Queue.cpp" and move the implementations of member functions in Queue.cpp to Queue.h because including .cpp is generally considered as bad because it may lead to linker error for duplicate definition, and temlate functions cannot be used if they are in different translation unit. 另外,您应该删除#include "Queue.cpp"并将#include "Queue.cpp"中成员函数的Queue.cpp移至Queue.h因为通常包括.cpp都被认为是不好的,因为它可能导致重复定义的链接器错误,而temlate函数不能如果它们位于不同的翻译单位中,则使用它们。

One more point: The comment says that 还有一点:评论说

flag=q.enqueue(11); // To test the returned value of the enqueue fnction. It returns flase when the q has no room

but actual implementation 但实际执行

//Add an entry to the beginning of the queue
template<class ItemType>
bool Queue<ItemType>::enqueue(const itemType &item) { 

    count++;
    items[count - 1] = item;

    return true;
}

doesn't do what is said in the comment and cause out-of-range access. 不执行注释中所说的操作,并导致超出范围的访问。 You should do one of these: 您应该执行以下操作之一:

  • Inclease MAX_SIZE to a value that is large enough. MAX_SIZE设置为足够大的值。
  • Implement error checking. 实施错误检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM