2010-12-02 74 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; 
}