2014-05-02 46 views
1

我在第7行有错误, *前我想让它通过学生证获得优先权对象的优先级队列,也没有必要有对象的指针可以对象本身在我的代码制作对象的优先队列

#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' 
+1

参考:C++已经有一个['的std :: priority_queue'(http://en.cppreference.com/w/cpp/container/priority_queue)。但是,如果你正在学习这一点,请继续关注它。 :) – cHao

回答

1

其实问题是,struct node不知道班级学生,因为它是后来定义的。解决方法是在节点之前声明Student,但是您可以将Student放入额外的标题中,并将该标题包含在节点标题中(我个人更喜欢这种方式)。

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

哦,我剪下粘贴的节点后,学生它的工作我很生气我自己我浪费了大约30分钟:/ –

+0

@ user3125340不要生气,这些都是发生的事情;)但我建议你至少每个类有一个头文件来组织你的代码,这样可以防止像这样的大多数错误... – Theolodis

0

你应该使用前向声明:

struct node; 
class Student; 

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

// . . .