gpt4 book ai didi

C++: [class] 未在此范围内声明

转载 作者:行者123 更新时间:2023-11-30 04:20:44 24 4
gpt4 key购买 nike

<分区>

--------------------Queue.h--------------------

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#include "MyException.h"

using namespace std;

template<class T>
class Queue;

template<class T>
ostream& operator<<(ostream&,Queue<T>&);

template<class T>
class Queue
{
public:
friend ostream& operator<< <T>(ostream&,Queue<T>&);
Queue();
Queue(const Queue<T>& other);
Queue<T>& operator=(const Queue<T>& other);
~Queue();
void enqueue(const T& el);
T dequeue();
void increasePriority(const T& el);
bool isEmpty();

private:
class Node
{
public:
Node(const T& data, Node* n = 0)
{
element = data;
next = n;
}

T element;
Node* next;
};
Node* head;

};

#include "Queue.C"

#endif

我们根本不允许更改 .h(以上)文件。

--------------------Queue.C--------------------

 #include "Queue.h"

template<class T>
ostream& operator << (ostream &strm, Queue<T> &obj)
{
if (obj.isEmpty())
{
strm << "[]";
return strm;
}
else
{
strm << '[';
//line 28
Node* tmp = new Node();
tmp = obj.head;
strm << tmp.element + ',';
while (tmp->next != 0)
{
tmp = tmp->next;
if (tmp-next != 0)
{
strm << tmp.element + ',';
}
else
{
strm << tmp.element;
}
}
strm << ']';
delete [] tmp;
tmp = 0;
return strm;
}
return strm;
}
//...more code
Queue::Queue()
{
//line 54
head = new Node();
}

因此从该代码中我收到的一些错误如下:

Queue.C: In function ‘std::ostream& operator<<(std::ostream&, Queue<T>&)’:
Queue.C:28: error: ‘Node’ was not declared in this scope
Queue.C:28: error: ‘tmp’ was not declared in this scope
Queue.C:28: error: expected type-specifier before ‘Node’
Queue.C:28: error: expected ‘;’ before ‘Node’
Queue.C:34: error: ‘next’ was not declared in this scope
Queue.C: At global scope:

Queue.C:54: error: ‘head’ was not declared in this scope
Queue.C:54: error: expected type-specifier before ‘Node’
Queue.C:54: error: expected ‘;’ before ‘Node’

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com