C++ 生成对象的优先级队列
我在第7行中有错误,该行丢失;在*之前,我想让对象的优先级队列通过学生ID获得优先级,并且不需要对象指针,它可以是对象本身C++ 生成对象的优先级队列,c++,C++,我在第7行中有错误,该行丢失;在*之前,我想让对象的优先级队列通过学生ID获得优先级,并且不需要对象指针,它可以是对象本身 #include<iostream> #include<string> using namespace std; struct node { int priority; Student * S; node * next; }; class Student { int ID; s
#include<iostream>
#include<string>
using namespace std;
struct node
{
int priority;
Student * S;
node * next;
};
class Student
{
int ID;
string name;
public:
Student()
{
cin>>ID;
cin>>name;
}
void out()
{
cout<<"ID is : "<<ID<<" "<<"Name is : "<<name<<endl;
}
};
class Priority_Queue
{
node * head;
//node * back;
public:
Priority_Queue()
{
head=NULL;
//back=NULL;
}
void push(Student * Q, int a)
{
node * p=new node;
p->next=NULL;
p->priority=a;
p->S=Q;
if(head==NULL)
head=p;
else
{
node * q=head;
node * r=NULL;
while(a<=q->priority)
{
r=q;
q=q->next;
}
r->next=p;
p->next=q;
}
}
Student * pop()
{
if(isempty())
{
cout<<"Empty"<<endl;
exit(1);
}
else
{
return head->S;
head =head->next;
}
}
bool isempty()
{
if(head==NULL)
return true;
else return false;
}
};
int main()
{
Student S1,S2,S3,S4;
return 0;
}
实际上问题是,
struct节点
不知道类student,正如后面定义的那样。一种解决方法是在节点之前声明Student,但您也可以将Student放在一个额外的头中,并将该头包含在节点头中(我个人更喜欢这种方式)
您应该使用转发声明:
struct node;
class Student;
struct node
{
int priority;
Student * S;
node * next;
};
// . . .
供参考:C++已经有了。但是,如果你这样做是为了学习,那就坚持下去。:)哦,我在它工作后剪切了粘贴的节点,我对自己非常生气,我在上面浪费了大约30分钟:/@user3125340别生气,这些都是发生的事情;)但我建议您通过每个类至少有一个头文件来组织代码,这样可以防止大多数类似这样的错误。。。
class Student;
struct node
{
int priority;
Student * S;
node * next;
};
struct node;
class Student;
struct node
{
int priority;
Student * S;
node * next;
};
// . . .