Создание класса очереди с использованием шаблонов для моего класса структур данных. Я получаю C2955 «Очередь - использование шаблона класса требует списка аргументов шаблона», когда я создаю, среди множества других ошибок, которые, я думаю, появляются из этого. Я сравнивал эту программу с другими, которые использовали шаблоны, и я не могу понять, что в этом по-другому. Может ли кто-нибудь помочь мне через это?Ошибка C2955 при кодировании класса шаблона очереди
//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;
}
В Queue.cpp нет явных синтаксических ошибок, которые я могу найти. Я знаю, что назад и спереди не используются, но я не уверен, что они предназначены для еще. Наш профессор предоставил нам большую часть файла заголовка и главного.
//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
В основном, когда «Queue q;» называются, чтобы сделать пустой объект очереди, есть красная строка «список аргументов для шаблона класса„Queue“отсутствуют
#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";
}
Других ошибок, которые я получаю при строительстве являются C2662 и C2244
Не '#include" Queue.cpp ". Никогда не включайте файлы '.cpp'. –
'#include" something.cpp "' обычно не подходит, потому что это может привести к ошибке компоновщика для множественного определения. – MikeCAT
Также не используйте 'namespace std;' в файле заголовка. Это очень плохая плохая плохая практика. –