2010-12-02 100 views
-1

的是自我學習的過程設計隊列這裏是代碼執行隊列

#include <iostream> 
using namespace std; 
template <class T> 
class Queue{ 

public: 
    T * q; 
    int n, head, tail; 
public: 
     Queue(int maxn){ 

      q=new T[maxn+1]; 
      n=maxn+1; head=n; 
      tail=0; 



     } 

     int emty() const { 
      return ((head%n)==tail); 


     } 
     void put(T k){ 
      a[tail++]=k; tail=tail%n; 


     } 
      T get(){ 
       head=head%n; return q[head++]; 

      } 
}; 
template <class T> 
int main(){ 

    Queue<int>a(10); 
    a.put(13); 
    a.put(45); 
    a.put(12); 
    a.put(10); 
    a.put(30); 
    a.put(45); 
    while(!a.emty()){ 
     cout<<a.get()<<" "; 


    } 

    return 0; 
} 

這裏是錯誤

1>------ Build started: Project: QUEUE, Configuration: Debug Win32 ------ 
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup 
1>D:\c++_algorithms\QUEUE\Debug\QUEUE.exe : fatal error LNK1120: 1 unresolved externals 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

請幫助

回答

3

main功能不能是一個模板。在刪除編譯錯誤之前,只需刪除template <class T>即可。

+5

定義`main`爲模板,真正顯示出巨大的不理解。希望OP使用一本好的C++書。 – 2010-12-02 15:54:20

+0

@Steve - 你可能想回讀他的其他問題:http://stackoverflow.com/questions/3406407/can-functions-be-in-a-struct。我們是他的C++書。 – 2010-12-07 19:08:03

0

定義這樣的主要功能:int main()。這是一個特殊的功能,在開始時運行並且不能更改它的簽名。

0
q=new T[maxn+1]; 
n=maxn+1; 
head=n; 
tail=0; 

創建類型T的大小爲MAXN數組+ 1

您僅可使用0到MAXN;

設置head = maxn+1可能會引起問題,因爲這是陣列

同樣的「\ 0」,

void put(T k) 
{ 
    a[tail++]=k; 
    tail=tail%n; 
} 

你在這裏做什麼是有點怪。你在[tail]上賦值k,然後增加尾部1,然後你將尾部劃分爲k與maxn + 1的餘數,如果我沒有弄錯,它將總是和尾部一樣?這不是多餘的?想象一下尾巴是2,而maxn是15,2%15 = 2。通常,你的整個方法有點奇怪。

也許你應該做一些數據結構研究。尋找鏈接列表。對這樣的結構使用數組並不是錯的,但也是不正確的。陣列滿了會發生什麼?在此之後,如何跟蹤陣列上所有新插入的元素(假設您騰出空間)以及如何知道哪些元素可以自由插入新的東西?

0
#include<iostream> 
using namespace std; 
template < class type > class que { 
    private: 
     type arr[10000]; 
     int front, rear; 
     public: 
     que() { 
      front = 0; 
      rear = 0; 
     } 
     void insertion(type data) ; 
     type deletion(); 
}; 

template<class type> type que<type>:: deletion() { 
    cout<<front<<endl; 
    return arr[front++]; 
} 
template<class type> void que<type>:: insertion(type data) { 
    cout<<rear<<" r"<<endl; 
    arr[rear++] = data; 
} 

int main() { 
    que<int> q; 
    q.insertion(12); 
    q.insertion(23); 
    q.insertion(22222); 
    cout<<q.deletion()<<endl; 
    cout<<q.deletion()<<endl; 
    cout<<q.deletion()<<endl; 
}