gpt4 book ai didi

c++ - 奇怪的错误我不知道该怎么办

转载 作者:行者123 更新时间:2023-11-28 07:01:10 25 4
gpt4 key购买 nike

hw3 双端队列.h

#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"

hw3 双端队列.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 )

“奇怪的错误”是因为,在 C++11 之前,类的作者实际表示不得复制该类的唯一方法是使其复制构造函数(和赋值运算符)private,因此无论如何尝试时都会出现此访问错误。

顺便说一句,您在所有这些定义中都缺少 DeQueType::,并且将您 #include 的文件称为“.cpp”是一种误导。是的,您需要在 header 中使用这些定义,但通常我们使用“.ipp”或其他一些扩展名来消除混淆。

关于c++ - 奇怪的错误我不知道该怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22442997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com