2014-05-02 49 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; 
}; 

// . . .