我所有其他重载的运算符都在工作。但是,我无法获得<<工作正常。
编译器说:
main.cpp中包含的文件:3:0:
Queue.h:在函数'std :: ostream&运算符<<(std :: ostream&,const Queue&)':
Queue.h:203:19:错误:未在此范围内声明'currNode'
这是我的代码:
Queue.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
的main.cpp
#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;
}
感谢您的帮助!
答案 0 :(得分:0)
我强烈建议您阅读hmjd发布的链接,但问题的关键在于您需要告诉编译器Queue :: Node是一种类型,通过typename Queue< T >::Node