【发布时间】:2021-10-23 11:18:29
【问题描述】:
我正在尝试从另一个队列复制一个队列,但该函数给出以下编译错误:
“类节点”没有名为“isEmpty”的成员
“类节点”没有名为“入队”的成员
“类节点”没有名为“getFirst”的成员
“类节点”没有名为“出队”的成员
'不能将'operations*' 转换为'Node*' 作为回报
#include<iostream>
#include "Queue.h"
using namespace std;
Node* copyQueue(Node* q1)
{
operations *q2 = new operations();
while(q1->isEmpty()!= True)
{
q2 -> enqueue( q1 -> getFirst() );
q1 -> dequeue();
}
return q2;
}
int main()
{
operations *q1 = new operations();
int temp, n;
cout << "Enter queue size: ";
cin >> n;
cout << "Enter data to copy: ";
for( int i = 0 ; i < n ; i++ )
{
cin >> temp;
q1 -> enqueue(temp);
}
copyQueue(q1);
cout << "Copied Queue = ";
q2 -> display();
}
queue.h 源代码:
#include<iostream>
#include "Node.h"
using namespace std;
class operations
{
public:
Node *front= NULL;
Node *newdata= NULL;
Node *rear= NULL;
void enqueue(int item)
{
newdata = new Node(item);
if(front==NULL && rear == NULL)
{
front = rear = newdata;
}
else
{
rear->next = newdata;
rear = newdata;
}
}
void dequeue()
{
Node *temp = front;
front = front->next;
delete temp;
}
bool isEmpty()
{
if(front == NULL)
return true;
else
return false;
}
int getFirst()
{
return front -> item;
}
void display()
{
Node *temp= front;
while(temp!= NULL)
{
cout<<temp->item<<"->";
temp= temp->next;
}
cout<<endl;
}
};
【问题讨论】:
-
还需要看看
Node是如何定义的。拼凑一个minimal reproducible example。 -
猜测一下,
copyQueue的参数类型是错误的。你传给它一个operations *,来自Node的缺失成员在operations。
标签: c++ class linked-list copy queue