我在第7行有错误;之前*我想要通过学生ID获得优先级的对象的优先级队列,并且没有必要有对象指针它可以是对象本身
#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;
}
我的代码中的错误
1>d:\codes\priority queue\priority queue\1.cpp(7): error C2143: syntax error : missing ';' before '*'
1>d:\codes\priority queue\priority queue\1.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\codes\priority queue\priority queue\1.cpp(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>d:\codes\priority queue\priority queue\1.cpp(41): error C2039: 'S' : is not a member of 'node'
1> d:\codes\priority queue\priority queue\1.cpp(5) : see declaration of 'node'
1>d:\codes\priority queue\priority queue\1.cpp(66): error C2039: 'S' : is not a member of 'node'
1> d:\codes\priority queue\priority queue\1.cpp(5) : see declaration of 'node'
答案 0 :(得分:1)
实际上问题是,struct node
不知道班级学生,因为它是在之后定义的。解决方法是在节点之前声明Student,但是你也可以将Student放在额外的标题中并在节点标题中包含该标题(我个人更喜欢这样。)。
class Student;
struct node
{
int priority;
Student * S;
node * next;
};
答案 1 :(得分:0)
你应该使用前瞻声明:
struct node;
class Student;
struct node
{
int priority;
Student * S;
node * next;
};
// . . .