クラステンプレートstack


とても葛藤して、とても憂鬱で、クラステンプレートとクラステンプレートの実現は一緒に置く必要があります.原因はなぜ、クラステンプレートがクラスではないようです.のええ、やはり一緒に書いてください.さもなくばとても時間を浪費して、1つの方式は通じることができて、どうして別の方式を探す必要がありますか?
テンプレートクラスstack.hファイル(.hファイルですが、テンプレートクラスの実装---定義が含まれています):
/******************************************************* 
 *     :
 *     :              (             ,          ) 
 *       :      stack     
 *     :                   
 * = :                  
 * empty():                 
 * push():                         
 * top():                 ,        
 * pop():                        
 * <<   display():          
 * 
 *     (                    ): 
 * 1.    (   )    myArray 0,1,...,myTop    
 * 2. -1 <= myTop <= myCapacity 
 *******************************************************/ 

#include <iostream>
#include <cassert>
#include<cstdlib>

#ifndef STACK_H
#define STACK_H

using namespace std;
template <typename T>

//           :

// ①.                     
// ②.          :              ,   ,   ,  、    ,       。    ::      
// ③.          。
class Stack
{
    public:
        Stack(int elementNumber = 128);
        //     
        virtual ~Stack();
        // copy constructor,       
        Stack(const Stack<T>& original);
        // assignment       
        const Stack<T>& operator=(const Stack<T>& original);

        bool empty() const;
        void push(const T& value) ;
        void display(ostream & out)const;
        T top() const;
        void pop();
    protected:
    private:
        int myCapacity,
            myTop;
        T * myArray;
};

#endif // STACK_H


#include<new>
#include<iostream>
template<typename T>
Stack<T>::Stack(int elemenetNumber)
{
    assert(elemenetNumber>0);
    myCapacity = elemenetNumber;        // 1
    myArray = new(nothrow) T[elemenetNumber];
    if(myArray){
        myTop = -1;
    }else{
        cerr<<"Inadequate memory to allocate...
"; exit(1); } } template<typename T> Stack<T>::~Stack() { delete [] myArray; } template<typename T> Stack<T>::Stack(const Stack<T>& original) :myCapacity(original.myCapacity),myTop(original.myTop) { myArray = new (nothrow) T[myCapacity]; if(myArray){ for(int position = 0; position<= myTop; ++position ){ myArray[position] = original.myArray[position]; } }else{ cerr << "InAdequate memeory to allocate....
"; exit(1); } } template<typename T> const Stack<T> & Stack<T>::operator=(const Stack<T>& rhs) { if (this == &rhs) return *this; // handle self assignment //assignment operator if(myCapacity!= rhs.myCapacity) delete[] myArray; myArray = new (nothrow)T[rhs.myCapacity]; if(myCapacity==0){ cerr<<"InAdequate memeory to allocate....
"; exit(1); } myCapacity = rhs.myCapacity; myTop = rhs.myTop; for(int pos = 0; pos<=myTop; ++pos){ myArray[pos] = rhs.myArray[pos]; } return *this; } template<typename T> bool Stack<T>::empty() const{ return (myTop == -1); } template<typename T> void Stack<T>::push(const T& value){ if(myTop+1<myCapacity){ // 5, 4 myTop++; myArray[myTop] = value; } } template<typename T> void Stack<T>::display(ostream & out)const{ for(int pos= myTop;pos>=0; pos--){ out<< myArray[pos] << "\t"; } } template<typename T> T Stack<T>::top() const{ if(!empty()){ return myArray[myTop]; }else{ cerr<<"Stack is empty...
"; T *temp = new (T);         T garbage = *temp;         delete temp;         return garbage; } } template<typename T> void Stack<T>::pop(){ if(myTop>=0){ myTop--; }else{ cerr <<"Stack is empty...
"; } } template<typename T> ostream &operator<<(ostream &out, const Stack<T> &st){ st.display(out); return out; }

テンプレートクラステストファイル:
#include <iostream>
#include "Stack.h"

using namespace std;

template <typename T>
void print(Stack<T> st){
    st.display(cout);
}

int main()
{
    int cap;
    cout<< "Enter stack capacity: ";
    cin >> cap;

    Stack<int> intSt;
    Stack<char> charSt;

    for(int i=1 ; i<cap; ++i){
        intSt.push(100*i);
    }
    cout<< intSt<<endl;

    for(char ch='A'; ch<'D'; ch++){
        charSt.push(ch);
    }
    cout<<charSt<<endl;

    cout<<"contents of stack intSt (via print): 
"; print(intSt); cout<<endl; Stack<int> i_Stack; i_Stack = intSt; cout<<"contents of stack i_Stack after i_stack = intSt(via print):
"; print(i_Stack);cout<<endl; cout<<"stack i_stack is empty()?"<<boolalpha<<i_Stack.empty()<<endl; cout<<"Top value in i_stack"<<i_Stack.top()<<endl; cout<<"Topping value in i_stack"<<endl; while(!i_Stack.empty()){ cout<<"
Topping i_stack"<<i_Stack.top()<<endl; i_Stack.pop(); cout<<"contents of stack i_stack: "<<i_Stack; } cout<<"i_stack is empty? "<< boolalpha << i_Stack.empty()<<endl; cout<<"Top value of i_stack is: "<<i_Stack.top()<<endl; cout<<"Another Popping: "; i_Stack.pop(); return 0; }

プログラム出力ファイル:
Enter stack capacity: 5
400     300     200     100
C       B       A
contents of stack intSt (via print):
400     300     200     100
contents of stack i_Stack after i_stack = intSt(via print
400     300     200     100
stack i_stack is empty()?false
Top value in i_stack400
Topping value in i_stack

Topping i_stack400
contents of stack i_stack: 300  200     100
Topping i_stack300
contents of stack i_stack: 200  100
Topping i_stack200
contents of stack i_stack: 100
Topping i_stack100
contents of stack i_stack: i_stack is empty? true
Stack is empty...
Top value of i_stack is: 137016
Another Popping: Stack is empty...

Process returned 0 (0x0)   execution time : 2.344 s
Press any key to continue.