2017-01-26 55 views
-1

我對模板概念很陌生,試圖製作一個通用堆棧,並嘗試使用平衡括號問題。當試圖構建代碼時,我在Visual Studio 2010中遇到了錯誤C2990。代碼如下。請告訴我一個解決方案,因爲我堅持這個問題,並且找不到解決方案。錯誤C2990:'節點':非類模板已經被聲明爲類模板

Extact錯誤: - 錯誤C2990: '節點':非類模板已被宣佈爲一個類模板

template<class T> 
struct node { 
    T data; 
    struct node * next; 
}; 

template<class T> 
class Stack { 
    struct node *top; 
public : 
    Stack() { 
     top = NULL; 
    } 
    void push(T value); 
    T pop(); 
    T peek(); 
    int isEmpty(); 
}; 

template<class T> 
void Stack<T>::push(T value) { 
    struct node *new_node = new node; 
    if(new_node == NULL) { 
     cout << "Stack Overflow" << endl; 
     return; 
    } 
    new_node->data = value; 

    if(top != NULL) { 
     new_node->next = top; 
     top = new_node; 
    } else { 
     top = new_node; 
     new_node->next = NULL; 
    } 
} 

template<class T> 
T Stack<T>::pop() { 
    if(top == NULL) { 
     cout << "Stack Underflow" << endl; 
     return ; 
    } 
    T value = top->data; 
    struct node *tmp = top; 
    top = top->next; 
    free(tmp); 
    return value; 
} 

template<class T> 
T Stack<T>::peek() { 
    if(top == NULL) { 
     cout << "Stack Underflow" << endl; 
     return ; 
    } 
    return top->data; 
} 

template<class T> 
int Stack<T>::isEmpty() { 
    if(top == NULL) 
     return TRUE; 
    return FALSE; 
} 

int balanced(char a, char b) { 
    switch(a) { 
    case '[' : 
     if(b == ']') 
      return TRUE; 
     return FALSE; 
    case '{' : 
     if(b == '}') 
      return TRUE; 
     return FALSE; 
    case '(' : 
     if(b == ')') 
      return TRUE; 
     return FALSE; 
    } 
} 

int isOpeningParenthesis(char a) { 
    if(a == '[' || a == '{' || a == '(') 
     return TRUE; 
    return FALSE; 
} 

int isClosingParenthesis(char a) { 
    if(a == ']' || a == '}' || a == ')') 
     return TRUE; 
    return FALSE; 
} 

int balancedParenthesis(char *arr, int length) { 
    int i; 
    Stack<char> *s = new Stack<char>; 
    for(i=0; i<length; i++) { 
     if(TRUE == isOpeningParenthesis(arr[i])) 
      s->push(arr[i]); 
     else if(TRUE == isClosingParenthesis(arr[i])){ 
      if((TRUE == s->isEmpty()) || (FALSE == balanced(s->pop(), arr[i]))) 
       return FALSE; 
     } 
    } 
    if(FALSE == s->isEmpty()) 
     return FALSE; 
    return TRUE; 
} 

int main() { 
    char a1[10] = {'{','[',']','(','(',')',')','[',']','}'}; 
    char a2[5] = {'[','[',']',']','}'}; 

    for(int i = 0; i < 10; i++) 
     cout << a1[i] << " "; 
    cout << endl; 
    if(TRUE == balancedParenthesis(a1, sizeof(a1)/sizeof(a1[0]))) 
     cout << "Balanced Parenthesis " << endl; 
    else 
     cout << "Not balanced parenthesis" << endl; 

    for(int i = 0; i < 5; i++) 
     cout << a2[i] << " "; 
    cout << endl; 
    if(TRUE == balancedParenthesis(a2, sizeof(a2)/sizeof(a2[0]))) 
     cout << "Balanced Parenthesis " << endl; 
    else 
     cout << "Not balanced parenthesis" << endl; 

    getch(); 
    return 0; 
} 
+0

而*哪裏*你得到的錯誤?請用例如顯示的代碼指明它。一條評論。 –

+0

在幾個不相關的筆記中,爲什麼在用C++編程時使用'int'和'TRUE'和'FALSE'? C++有一個本地'bool'類型,可以是'true'或'false'。你的''''''''''''''''''''''''''''''''''''''''''''''''''''你不要在C++中用'new'創建對象,聲明一些普通的變量可以工作得很好(所以你可以做例如'Stack s;')。最後,例如'return top == NULL'將會工作,如果表達式爲真,返回'true';否則返回'false',不需要'if'。 –

+0

爲什麼使用'int'('TRUE','FALSE')而不是'bool'? –

回答

0

在代碼:

template<class T> 
class Stack { 
    struct node *top; 

它應該是:

template<class T> 
class Stack { 
    node<T> *top; 

避免在不需要的地方重複關鍵字struct是一種很好的風格。稍後在代碼中再次發生這個相同的錯誤。此外,您的代碼還有其他各種錯誤,但是這是您錯誤消息的原因。

注意:將來不要在問題中發佈不相關的代碼,並指出錯誤消息發生在哪一行。