奇怪的错误,我不知道该怎么做

Weird error I have no idea what to do

本文关键字:我不知道 错误      更新时间:2023-10-16

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;
}
相关文章: