#include <iostream>
template <class ItemType>
struct NodeType;
template <class ItemType>
class DeQueType
{
public:
DeQueType();
// Class constructor.
// Because there is a default constructor, the precondition that the
// queue has been initialized is omitted.
~DeQueType();
// Class destructor.
void MakeEmpty();
// Function: Initializes the queue to an empty state.
// Post: Queue is empty.
bool IsEmpty() const;
// Function: Determines whether the queue is empty.
// Post: Function value = (queue is empty)
bool IsFull() const;
// Function: Determines whether the queue is full.
// Post: Function value = (queue is full)
void EnqFront(ItemType newItem);
// Function: Adds newItem to the front of the queue.
// Pre: Queue is not full.
// Post: newItem is at the front of the queue.
void EnqRear(ItemType newItem);
// Function: Adds newItem to the rear of the queue.
// Pre: Queue is not full.
// Post: newItem is at the rear of the queue.
void DeqFront(ItemType& item);
// Function: Removes front item from the queue and returns it in item.
// Pre: Queue is not empty.
// Post: Front element has been removed from the queue.
// item is a copy of the removed element.
void DeqRear(ItemType& item);
// Function: Removes rear item from the queue and returns it in item.
// Pre: Queue is not empty.
// Post: Rear element has been removed from the queue.
// item is a copy of the removed element.
void Print( std::ostream out );
// Function: Prints items in the deque from front to rear.
// Deque is printed on a single line of output with one space between each item.
// Pre: Deque has been initialized.
// Post: Deque is unchanged.
int Length();
// Function: Returns the number of items in the deque.
// Pre: Deque has been initialized.
// Post: Function value = number of items in the deque.
// Deque is unchanged.
private:
NodeType<ItemType>* dequeFront;
NodeType<ItemType>* dequeRear;
};
#include "hw3 DeQue.cpp"
#include <cstddef> // For NULL
template <class ItemType>
struct NodeType
{
ItemType info;
NodeType* next;
};
template <class ItemType>
DeQueType<ItemType>::DeQueType() // Class constructor.
// Post: dequeFront and dequeRear are set to NULL.
{
dequeFront = NULL;
dequeRear = NULL;
}
template <class ItemType>
void DeQueType<ItemType>::MakeEmpty()
// Post: DeQueue is empty; all elements have been deallocated.
{
NodeType<ItemType>* tempPtr;
while (dequeFront != NULL)
{
tempPtr = dequeFront;
dequeFront = dequeFront->next;
delete tempPtr;
}
dequeRear = NULL;
}
template <class ItemType> // Class destructor.
DeQueType<ItemType>::~DeQueType()
{
MakeEmpty();
}
template <class ItemType>
bool DeQueType<ItemType>::IsFull() const
// Returns true if there is no room for another ItemType on the free store;
// false otherwise.
{
NodeType<ItemType>* ptr;
ptr = new NodeType<ItemType>;
if (ptr == NULL)
return true;
else
{
delete ptr;
return false;
}
}
template <class ItemType>
bool DeQueType<ItemType>::IsEmpty() const
// Returns true if there are no elements on the DeQueue; false otherwise.
{
return (dequeFront == NULL);
}
//template <class ItemType>
//void DeQueType<ItemType>::Enqueue(ItemType newItem)
//// Adds newItem to the rear of the DeQueue.
//// Pre: DeQueue has been initialized and is not full.
//// Post: newItem is at rear of DeQueue.
//
//{
// NodeType<ItemType>* newNode;
//
// newNode = new NodeType<ItemType>;
// newNode->info = newItem;
// newNode->next = NULL;
// if (dequeRear == NULL)
// dequeFront = newNode;
// else
// dequeRear->next = newNode;
// dequeRear = newNode;
//}
//template <class ItemType>
//void DeQueType<ItemType>::DeQueue(ItemType& item)
//// Removes front item from the DeQueue and returns it in item.
//// Pre: DeQueue has been initialized and is not empty.
//// Post: Front element has been removed from DeQueue.
//// item is a copy of removed element.
//{
/* NodeType<ItemType>* tempPtr;
tempPtr = dequeFront;
item = dequeFront->info;
dequeFront = dequeFront->next;
if (dequeFront == NULL)
dequeRear = NULL;
delete tempPtr;*/
//}
template <class ItemType>
void EnqFront(ItemType newItem)
// Function: Adds newItem to the front of the queue.
// Pre: Queue is not full.
// Post: newItem is at the front of the queue.
{
NodeType<ItemType>* newNode;
newNode = new NodeType<ItemType>;
newNode->info = newItem;
newNode->next = NULL;
if (dequeRear == NULL)
dequeFront = newNode;
else
dequeFront->next = newNode;
dequeFront = newNode;
}
template <class ItemType>
void EnqRear(ItemType newItem)
// Function: Adds newItem to the rear of the queue.
// Pre: Queue is not full.
// Post: newItem is at the rear of the queue.
{
NodeType<ItemType>* newNode;
newNode = new NodeType<ItemType>;
newNode->info = newItem;
newNode->next = NULL;
if (dequeRear == NULL)
dequeFront = newNode;
else
dequeRear->next = newNode;
dequeRear = newNode;
}
template <class ItemType>
void DeqFront(ItemType& item)
// Function: Removes front item from the queue and returns it in item.
// Pre: Queue is not empty.
// Post: Front element has been removed from the queue.
// item is a copy of the removed element.
{
NodeType<ItemType>* tempPtr;
tempPtr = dequeFront;
item = dequeFront->info;
dequeFront = dequeFront->next;
if (dequeFront == NULL)
dequeRear = NULL;
delete tempPtr;
}
template <class ItemType>
void DeqRear(ItemType& item)
// Function: Removes rear item from the queue and returns it in item.
// Pre: Queue is not empty.
// Post: Rear element has been removed from the queue.
// item is a copy of the removed element.
{
NodeType<ItemType>* tempPtr;
tempPtr = dequeRear;
item = dequeRear->info;
dequeFront = dequeRear->next;
if (dequeRear == NULL)
dequeRear = NULL;
delete tempPtr;
}
template <class ItemType>
void Print( std::ostream out ) //cause of the problem
// Function: Prints items in the deque from front to rear.
// Deque is printed on a single line of output with one space between each item.
// Pre: Deque has been initialized.
// Post: Deque is unchanged.
{
out << dequeFront->info << endl;
while(dequeFront->next != NULL)
out << dequeFront->next << endl;
}
template <class ItemType>
int Length()
// Function: Returns the number of items in the deque.
// Pre: Deque has been initialized.
// Post: Function value = number of items in the deque.
// Deque is unchanged.
{
return count;
}
ошибка
Error 1 error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios': cannot access private member declared in class std::basic_ios<_Elem,_Traits>' c:\program files (x86)\microsoft visual studio 10.0\vc\include\ostream 604
Вы можете мне помочь?
template <class ItemType>
void Print( std::ostream out ) //cause of the problem
Потоки не копируются. Они не являются контейнерами; это потоки данных. Потоки не могут быть скопированы.
Возьмите поток по ссылке, вместо этого:
template <class ItemType>
void Print( std::ostream& out )
"Странная ошибка" заключается в том, что до С++ 11 единственным способом для автора класса фактически обозначить, что класс не должен быть скопирован, заключается в том, чтобы сделать его конструктор копирования (и оператор присваивания) private
, вы получаете эту ошибку доступа, когда пытаетесь в любом случае.
Кстати, вам не хватает DeQueType::
во всех этих определениях, а вызов файла, который вы #include
.Cpp, вводит в заблуждение. Да, вам нужны эти определения в заголовке, но обычно мы используем ".ipp" или какое-то другое расширение, чтобы устранить путаницу.
Вы не объявляли определенные методы как члены DeQueType
поэтому компоновщик не сможет разрешить эти символы для следующего:
EnqFront
EnqRear
DeqFront
DeqRear
Print
Length
Эти методы не смогут получить доступ к приватным методам DeQueType
а в случае с EnqFront
он не будет знать, что делать или к какой ссылке:
Чтобы исправить, добавьте следующие определения:
DeQueType<ItemType>::MethodName(args)
BTW: комментарии Doxygen идут выше строки шаблона следующим образом:
/** @brief Adds newItem to the front of the queue.
* Pre: Queue is not full.
* Post: newItem is at the front of the queue.
*/
template <class ItemType>
void DeQueType<ItemType>::EnqFront(ItemType newItem)
{
// DEfinition
}
Вы также должны прислушаться к советам @rici о том, что может произойти с методом печати
Вот методы с включением области
template <class ItemType>
void DeQueType<ItemType>::EnqRear(ItemType newItem)
// Function: Adds newItem to the rear of the queue.
// Pre: Queue is not full.
// Post: newItem is at the rear of the queue.
{
NodeType<ItemType>* newNode;
newNode = new NodeType<ItemType>;
newNode->info = newItem;
newNode->next = NULL;
if (dequeRear == NULL)
dequeFront = newNode;
else
dequeRear->next = newNode;
dequeRear = newNode;
}
template <class ItemType>
void DeQueType<ItemType>::DeqFront(ItemType& item)
// Function: Removes front item from the queue and returns it in item.
// Pre: Queue is not empty.
// Post: Front element has been removed from the queue.
// item is a copy of the removed element.
{
NodeType<ItemType>* tempPtr;
tempPtr = dequeFront;
item = dequeFront->info;
dequeFront = dequeFront->next;
if (dequeFront == NULL)
dequeRear = NULL;
delete tempPtr;
}
template <class ItemType>
void DeQueType<ItemType>::DeqRear(ItemType& item)
// Function: Removes rear item from the queue and returns it in item.
// Pre: Queue is not empty.
// Post: Rear element has been removed from the queue.
// item is a copy of the removed element.
{
NodeType<ItemType>* tempPtr;
tempPtr = dequeRear;
item = dequeRear->info;
dequeFront = dequeRear->next;
if (dequeRear == NULL)
dequeRear = NULL;
delete tempPtr;
}
template <class ItemType>
void DeQueType<ItemType>::Print( std::ostream &out ) //cause of the problem
// Function: Prints items in the deque from front to rear.
// Deque is printed on a single line of output with one space between each item.
// Pre: Deque has been initialized.
// Post: Deque is unchanged.
{
out << dequeFront->info << endl;
while(dequeFront->next != NULL)
out << dequeFront->next << endl;
}
template <class ItemType>
int DeQueType<ItemType>::Length()
// Function: Returns the number of items in the deque.
// Pre: Deque has been initialized.
// Post: Function value = number of items in the deque.
// Deque is unchanged.
{
return count;
}
#include "hw3 Deque.h"
, а не наоборот.