c++ - 制作对象的优先队列

标签 c++

我在第 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'

最佳答案

实际上问题在于,struct node 不知道类 student,因为它是在后面定义的。一种解决方法是在节点之前声明 Student,但您也可以将 Student 放在一个额外的 header 中,并将该 header 包含在节点 header 中(我个人更喜欢这种方式。)。

class Student;
struct node        
{
    int priority;
    Student * S;
    node * next;
};

关于c++ - 制作对象的优先队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23420891/

相关文章:

Java 从 DLL 实例化 C++ 类

c++ - 简化 __VA_ARGS__ 问题 : cannot extract NULL

c++ - 如何只获取USB设备而不是系统中的所有设备

c++ - 为什么 STL 算法 for_each 两次调用我的仿函数的析构函数?

c++ - 伽罗华域算法的实现

c++ - 在类定义的开头这个宏是做什么用的?

c++ - 指向引用的指针是否指向引用的地址或值?

C++ RVO : when it happens?

c++ - 访问和存储/解析 std::chrono::duration::milliseconds (cpprest) 时使用什么类型

c# - 为 C# 使用 swig 时出现 TypeInitializationException