0

我所有其他重载的运算符都在工作。但是,我无法让 << 正常工作。

编译器说:

在 main.cpp:3:0 包含的文件中:

Queue.h:在函数'std::ostream& operator<<(std::ostream&, const Queue&)'中:

Queue.h:203:19:错误:未在此范围内声明“currNode”

这是我的代码:

队列.h

#ifndef QUEUE_H
#define QUEUE_H
#include <string>
#include <iostream>

using namespace std;

template <typename T>

class Queue
{
  template <typename V>
  friend ostream& operator<<(ostream&, const Queue<V>&);

  class Node
  {
    friend class Queue;
    public:
      T* data;
      Node* next;
  };

  public:
    Queue();
    Queue(Queue<T>&);
    ~Queue();
    void pushBack(T*);
    void popFront();
    T front();
    bool empty();

    T*    operator[](int);           
    Queue<T>&  operator+=(T*);            
    Queue<T>&  operator+=(Queue<T>&);     
    Queue<T>   operator+(T*);             
    Queue<T>   operator+(Queue<T>&);  
    Queue<T>&  operator-=(T*);            
    Queue<T>&  operator-=(Queue<T>&);     
    Queue<T>   operator-(T*);             
    Queue<T>   operator-(Queue<T>&);     
    Queue<T>&  operator=(const Queue<T>&);
    //Empty the queue
    Queue<T>   operator!();                


  private:
    Node* head;
};

//Constructor
template <typename T>
Queue<T>::Queue(): head(0)
{
}

template <typename T>
Queue<T>::Queue(Queue<T>& copy)
{
  head=0;
  Node* currNode = copy.head;
  Node* prevNode = 0;
  Node* newNode;

  while (currNode!=0){
    newNode = new Node();
    newNode->data = currNode->data;
    if (prevNode!=0) prevNode->next = newNode;
    if (head ==0) head = newNode;
    prevNode=newNode;
    currNode=currNode->next;
  }
}

//Destructor
template <typename T>
Queue<T>::~Queue()
{
  Node *currNode, *nextNode;
  currNode = head;
  while(currNode!=0){
    nextNode=currNode->next;
    delete currNode;
    currNode=nextNode;
  }
}

//Adding to back
template <typename T>
void Queue<T>::pushBack(T *newData){
  Node *newNode = new Node;
  newNode->data = newData;
  newNode->next = 0;


  //If empty
  if (head==0){
    head = newNode;
    return;
  }

  Node *currNode;
  currNode = head;
  while(currNode->next!=0)
    currNode=currNode->next;
  currNode->next = newNode;
}

//Removing from front
template <typename T>
void Queue<T>::popFront(){
  Node *oldNode;
  oldNode = head;

  head = oldNode->next;
  delete oldNode;
}

//getFront
template <typename T>
T Queue<T>::front(){
  return *(head->data);
}

//Is it empty
template <typename T>
bool Queue<T>::empty(){
  if (head==0)
    return true;
  else
    return false;
}

/*------------------------Overloaded Operators---------------------------*/

template <typename T>
Queue<T>& Queue<T>::operator+=(Queue<T>& q)
{

  Node *curr;
  curr = q.head;

  while(curr!=0)
  {
    this->pushBack(curr->data);
    curr = curr->next;
  }

  return (*this);
}

template <typename T>
Queue<T> Queue<T>::operator+(Queue<T>& q)
{
  Queue<T> newQueue = *this;

  newQueue+=q;

  return newQueue;
}

template <typename T>
Queue<T> Queue<T>::operator+(T *t)
{

  Queue<T> tmp = *this;

  if(t == 0)
   return tmp;

  tmp.pushBack(t);
  return tmp;
}

template <typename T>
Queue<T>& Queue<T>::operator+=(T *t)
{
  if(t == 0)
    return (*this);

  this->pushBack(t);
  return (*this);
}

template <typename T>
Queue<T> Queue<T>::operator!()
{
  if(this ==0)
   return (*this);  

  Node *curr = head;
  while(curr!=0)
  {
    curr = curr->next;
    this->popFront();
  }
  return (*this);
}

/*NOT WORKING?!?!*/
template <typename T>
ostream& operator<<(ostream& output, const Queue<T>& q)
{
  Queue<T>::Node *currNode;    /******HERE IS THE PROBLEM******/
  currNode = q.head;

  if(q.head == 0)
  {
    output << "Empty" << endl; 
    return output;
  }
  do {
    output << *(currNode->data) << endl;
    currNode = currNode->next;
  } while (currNode != 0);

  return output;
}


template <typename T>
Queue<T>& Queue<T>::operator-=(T *t)
{

  if(t == 0)
    return (*this);

  //REMOVE ELEMENT
  Node *currNode, *prevNode;
  currNode=head;
  prevNode=0;
  while (currNode != 0){
    if ((*currNode->data) == (*t)){
      if (prevNode == 0)
        head = currNode->next;
      else
        prevNode->next = currNode->next;
      delete currNode;
      break;
    }
    prevNode=currNode;
    currNode=currNode->next;
  }
  return (*this);
}

template <typename T>
Queue<T>& Queue<T>::operator-=(Queue<T>& q)
{
  //REMOVE ELEMENT
  Node *currNode;
  currNode=q.head;
  while(currNode!=0)
  {
    (*this)-=currNode->data;
    currNode=currNode->next;
  }
  return (*this);
}

template <typename T>
Queue<T> Queue<T>::operator-(T *t)
{
  //REMOVE ELEMENT
  Queue<T> newQueue = *(this);
  newQueue-=t;
  return newQueue;
}

template <typename T>
Queue<T> Queue<T>::operator-(Queue<T>& q)
{
  //REMOVE ELEMENT
  Queue<T> newQueue= *(this);
  newQueue-=q;
  return newQueue;
}

template <typename T>
Queue<T>& Queue<T>::operator=(const Queue<T>& copy)
{
  head=0;
  Node* currNode = copy.head;
  Node* prevNode = 0;
  Node* newNode;

  while (currNode!=0){
    newNode = new Node();
    newNode->data = currNode->data;
    if (prevNode!=0) prevNode->next = newNode;
    if (head ==0) head = newNode;
    prevNode=newNode;
    currNode=currNode->next;
  }
  return (*this);
}

template <typename T>
T* Queue<T>::operator[](int index)
{
  Node *currNode;
  currNode=head;
  int i=0;

  while(currNode!=0){
    if(index==i++)
      return currNode->data;
    currNode=currNode->next;
  }

  return 0;
}

#endif

主文件

#include <iostream>
#include <string>
#include "Queue.h"

using namespace std;

int main()
{

  Queue<string> q1;
  Queue<int> q2;

  string s = "Hello";
  int i = 3;

  q1.pushBack(&s);
  q2.pushBack(&i);

  cout<<"contents of q1 = " << (*q1) << endl;
  cout<<"contents of q2 = " << (*q2) << endl;


  return 0;
}

感谢您的帮助!

4

1 回答 1

0

我强烈建议您阅读 hmjd 发布的链接,但问题的要点是您需要告诉编译器 Queue::Node 是一种类型,通过typename Queue< T >::Node

于 2013-03-29T20:15:09.547 回答