2012-06-26 49 views
1
template<class T> 
class Stack 
{ 
public: 
    Stack() 
    { 
     cout<<"Enter size\n"; 
     cin>>size; 
     //stackPtr=new int[size]; 
     stackPtr= new T[size]; 
     top =0; 
    } 

    ~Stack() 
    { 
     delete[] stackPtr; 
    } 

    void display() 
    { 
     cout<<"*****************************\n"; 
     for(int i=0;i<top;i++) 
      cout<<stackPtr[i]<<endl; 
     cout<<"*****************************\n"; 
    } 

    bool Push() 
    { 
     int a; 
     cout<<"Enter element\n"; 
     cin>>a; 
     if(isFull()==false) 
     { 
      stackPtr[top++]=a; 
      return true; 
     } 
     else 
      return false; 
    } 

    bool Pop() 
    { 
     if(isEmpty()==false) 
     { 
      top--; 
      return true; 
     } 
     else 
      return false; 
    } 

    bool isEmpty() 
    { 
     if(top==0) 
      return true; 
     else 
      return false; 
    } 

    bool isFull() 
    { 
     if(top==size) 
      return true; 
     else 
      return false; 
    } 

private: 
    //int *stackPtr; 
    T *stackPtr; 
    int size; 
    int top; 
    //location of the last element added, -1 means empty stack 
}; 



void main() 
{ 

    int typeArray; 
    cout<<"What type of array do you want\n1.Integer\n2.Character\n"; 
    cin>>typeArray; 
    //If instead of these if statements, i simply 
     //write Stack<int> Stacks, everything works fine 
     //So i think theres something wrong with using char or float or ANY other 
     //datatype? 

    if(typeArray==1) 
     Stack <int>Stacks; 
    else 
     Stack <float>Stacks; 

    cout<<"1.PUsh\n2.Pop\n3.Display all\n4.exit\n"; 
    int ch=1; 
    cin>>ch; 
    while(ch!=-1) 
    { 

     switch(ch) 
     { 
     case 1: if(Stacks.Push()) 
      { 
       cout<<"Stack Full\nPop to enter other  values\n"; 

      } 
      break; 
     case 2: if(Stacks.Pop()==false) 
        cout<<"Stack EMpty\n"; 
      break; 
     case 3: Stacks.display(); 
      break; 
     case 4: exit(0); 
     default:cout<<"Reenter you option\n"; 
      break; 
     } 
     cout<<"1.PUsh\n2.Pop\n3.Display all\n4.exit\n"; 
     cin>>ch; 
    } 
} 

這些是我得到的錯誤。 我使用VS 2010C++ Basic中的類模板

test.cpp(104): error C2065: 'Stacks' : undeclared identifier 
test.cpp(104): error C2228: left of '.Push' must have class/struct/union type 
          is ''unknown-type'' 
test.cpp(110): error C2065: 'Stacks' : undeclared identifier 
test.cpp(110): error C2228: left of '.Pop' must have class/struct/union type 
          is ''unknown-type'' 
test.cpp(113): error C2065: 'Stacks' : undeclared identifier 
test.cpp(113): error C2228: left of '.display' must have class/struct/union type 
          is ''unknown-type'' 
+3

請修復那個可怕的縮進... – Nim

+1

你有沒有想自己修復它?你有沒有在錯誤信息中看到? –

回答

6
if(typeArray==1) 
    Stack <int>Stacks; 
else 
    Stack <float>Stacks; 

這段代碼創建了變量Stacks終身和範圍僅限於if聲明。該程序的其餘部分不可見。

您需要重新考慮您的設計。

而且,我在這裏看到一個邏輯錯誤:

bool Push() 
{ 
    int a; 
    cout<<"Enter element\n"; 
    cin>>a; 

不應該說是T而不是int?這樣,無論模板專業化如何,您只需閱讀int即可。

+0

好吧,我糾正了整數部分。 但說我想根據用戶輸入創建一個模板,那麼我如何實現它呢? – SLearner

+1

@slash:對於你的模板'T'需要在編譯時知道,所以你可以在某個表中創建所有可能的類型,並稍後使用與用戶請求類型相對應的類型。那麼你也需要一些方法來從用戶那裏獲得類型。 –

+0

@honk所以你的意思是說,我創建... 堆棧一個, 堆棧一個,等等?我完全明白 – SLearner