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

时间:2014-03-16 21:04:11

标签: c++

hw3 Deque.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.0\vc\include\ostream 604

你能帮助我吗?

2 个答案:

答案 0 :(得分:1)

template <class ItemType>
void Print( std::ostream out ) //cause of the problem

Streams不可复制。它们不是容器;它们是数据流。流量无法复制。

以参考方式取代流:

template <class ItemType>
void Print( std::ostream& out )

&#34;奇怪的错误&#34;是因为,在C ++ 11之前,类的作者实际上表示不能复制类的唯一方法是创建其复制构造函数(和赋值运算符)private,以便您无论如何都要尝试这个访问错误。

顺便说一句,您在所有这些定义上都缺少DeQueType::,并且调用了#include&#34; .cpp&#34;是误导。是的,您需要在标题中使用这些定义,但通常我们使用&#34; .ipp&#34;或其他一些延伸,以减轻混乱。

答案 1 :(得分:0)

您没有将某些方法声明为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;
}