错误C2990:'node':非类模板已被声明为类模板

时间:2017-01-26 11:45:38

标签: c++

模板概念的新手,并试图制作通用堆栈,并尝试平衡括号问题。尝试构建代码时,我在Visual Studio 2010中收到错误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; } 。代码如下。请建议我一个解决方案,因为我遇到了这个问题,并且无法找到解决方案。

  

Extact错误: - 错误C2990:'node':非类模板已被声明为类模板

<html>
    <head>
        <style>
            @font-face{
                font-family:fontName;
                src:url(https://fonts.gstatic.com/s/varelaround/v6/APH4jr0uSos5wiut5cpjrugdm0LZdjqr5-oayXSOefg.woff2)
            }
            body{
                font-family:fontName;
                font-size:2em;
                text-align:center;
            }
        </style>
    </head>
    <body>
        <h1>Title</h1>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

在代码中:

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

它应该是:

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

避免重复不需要的关键字struct是一种好方法。您稍后会在代码中再次发出同样的错误。此外,您的代码还有其他各种错误,但是这个错误是导致错误消息的原因。

NB。将来不要在问题中发布不相关的代码,并指出错误消息发生在哪一行。