奇怪的错误,我不知道该怎么做
Weird error I have no idea what to do
hw3 dequeue .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 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.0vcincludeostream 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"是误导的。是的,您需要在标题中使用这些定义,但通常我们使用"。
您没有将某些方法声明为DeQueType
的成员,因此链接器将无法解析以下符号:
-
EnqFront
-
EnqRear
-
DeqFront
-
DeqRear
-
Print
-
Length
这些方法将无法访问DeQueType
的私有方法,在EnqFront
的情况下,它将不知道该做什么或链接到什么:
要修复,在定义中添加以下内容:
DeQueType<ItemType>::MethodName(args)
BTW:氧注释在模板行上方,像这样:
/** @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
}
对于print方法
可能发生的情况,您还应该听取@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;
}
相关文章:
- 我不知道导致错误的原因 (C3074)
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- 函数抛出seg错误,我不知道为什么
- 尝试通过多个函数移动数组,但遇到了我不知道如何修复的错误
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- "错误:在'.标记'之前出现预期的主表达式,我不知道为什么
- 我正在制作c ++代码,但发生错误我不知道为什么
- std::cout << glGetString(GL_RENDER) << std::endl;抛出错误但不GL_Renderer或GL_Verision,我不知道为什么?
- 语法检查器指出一些我不知道为什么的错误
- 编写蒙特卡洛集成程序,我不知道为什么我会收到某些错误
- C++代码出现分段错误,我不知道为什么
- 我不知道如何使用 boost,虽然我的老师确实提供了代码,但我遇到了一个错误
- 我同时使用了莱布尼茨和瓦利斯公式来估算π但输出数字错误,我不知道我哪里出错了
- 我的一堆函数出现"undefined reference"错误,我不知道为什么
- 错误 C2064 — 我不知道我做错了什么
- Mingw32-w64 GCC 4.9.2 正则表达式错误,或者我不知道C++
- 错误数组用作初始值设定项,我不知道错误
- 尝试对结构向量进行排序时,我在算法文件中收到 20 个错误。我不知道怎么了?
- 删除数组时出现问题(我不知道这个错误)
- 我的返回语句有一个错误,我不知道要修复