2012-04-13 183 views
0

我正在创建一个自定义堆栈以便与使用堆栈库的旧类一起使用。错误C2955:'CELL':使用类模板需要模板参数列表C++ Stack

在编译我收到以下错误:

错误C2955:“CELL”:使用类模板需要模板参数列表C++堆栈

它编译.h文件包含直到我改变了堆(C++库)以my_Stack(自定义堆栈)

作为随后对H的代码

#ifndef generic_stack_H 
#define generic_stack_H 
#include <iostream> 
using namespace std; 

/************************************************************************** 
         Structure CELL has two fields. 
------------------  ------------------ 
| element | next | -> | element | next | 
------------------  ------------------ 
**************************************************************************/ 
template <class T> 
struct CELL { T element; struct CELL *next; }; 

/************************************************************************** 
           Class STACK. 
**************************************************************************/ 
template <class T> 
class my_STACK 
{ 
private: CELL *topCellPtr; 
public: 
    //----------------------------Constructor----------------------------- 
    my_STACK() {topCellPtr = NULL;} 
    //--------------------------Member functions-------------------------- 
void push(T e) 
{ 
    struct CELL *cP; 

    cP = new CELL; 
    cP->element = e; 
    cP->next = NULL; 
    if(topCellPtr != NULL) 
     cP->next = topCellPtr; 
    topCellPtr = cP; 

} 

void pop() 
{ 
    struct CELL *cP; 

    if(topCellPtr == NULL) 
    { 
     cout << "Trying to pop an empty stack." << endl; 
     return; 
    } 
    cP = topCellPtr; 
    topCellPtr = topCellPtr->next;  
} 

bool empty() { 

    bool check false; 

    struct CELL *cP; 

    if(topCellPtr == NULL) 
    { 

     check = true; 

    } 

    return true; 

} 


T top() {return topCellPtr->element;} 

void print() 
{ 
    struct CELL *cP = topCellPtr; 

    cout << "Stack (top = left): "; 
    while (cP != NULL) 
    { 
     cout << cP->element << " -> "; 
     cP = cP->next; 
    } 
    cout << endl;  
} 

int Count_Occurences (int e) { 

    int count = 0; 

    struct CELL *cP = topCellPtr; 

    while(cP != NULL) { 

     if(cP->element == e) { 

      count++; 

     }//end if 

     cP = cP->next; 

    }//end while 

    return count; 

} 

void Reverse_Stack() { 

struct CELL *nP = NULL; 

struct CELL *newtopCellPtr = nP; 

while(topCellPtr != NULL) { 

    int n = this->top(); 

    this->pop(); 

    nP = new CELL; 
    nP->element = n; 
    nP->next = NULL; 

    if(newtopCellPtr != NULL) { 

     nP->next = newtopCellPtr; 

    } 

    newtopCellPtr = nP; 

}//end while 

topCellPtr = newtopCellPtr; 

} 

int nth(int index) { 

    struct CELL *cP = topCellPtr; 

    for(int i=1; i < index; i++) { 

     cP = cP->next; 

    }//end for 

    return cP->element; 

} 

void Append (my_STACK anotherStack) { 

    anotherStack.Reverse_Stack(); 

    while(anotherStack.topCellPtr != NULL) { 

     this->push(anotherStack.top()); 

     anotherStack.pop(); 

    }// 

end while 

    } 

}; 

#endif 

我的主要功能是为FO llowed:

#include <iostream> /*input output stream... 
        print on screen... keyboard input*/ 

//#include <stack> //stack implementaiton 

#include <ctype.h> 

#include <string> 

#include "generic_stack.h" 

using namespace std;/*instead of specifically typing 
        std::cout we can simply write cout 
        this package also includes stack 
        container*/ 

enum state {FRESH, ID, NUM, ICT, LESS, GREATER, LESSDASH, PLUS, MINUS, ERR}; 

void reverse(my_STACK<char> &s) { 

    my_STACK<char> s2; 

    while(!s.empty()) { 

     s2.push(s.top()); 

     s.pop(); 

    } 

    s = s2; 

} 

void Reverse_Stack_Stack(my_STACK< my_STACK<char> > &s) { 

    my_STACK< my_STACK<char> > s2; 

    while(!s.empty()) { 

     s2.push(s.top()); 

     s.pop(); 

    } 

    s = s2; 

} 


void newLine(my_STACK<char> &P , state &current_state) { 

    current_state = FRESH; 

    while(!P.empty()) { 

     P.pop(); 

    } 

} 

bool isToken(state s) { 

    if(s == FRESH) { 

     return false; 

    } else if (s == ERR) { 

     return false; 

    } else { 

     return true; 

    } 

} 

bool canPush(char s , state current_state) { 

    if(current_state == FRESH) { 

     return true; 

    } else 

     if(current_state == NUM) { 

      if(isdigit(s)) { 

       return true; 

      } else { 

       return false; 

      } 

    } else 

     if(current_state == ID) { 

      if(isalpha(s)) { 

       return true; 

      } else { 

       return false; 

      } 

     } else 

      if(current_state == LESSDASH) { 

     if(s == '>') { 

      return true; 

     } else { 

      return false; 

     } 

    }/*end lessdash*/ else 

    if(current_state == LESS) { 

     if(s == '=' || s == '-' || s == '>') { 

      return true; 

     } else { 

      return false; 

     } 

    }/*end less */ else 

    if(current_state == GREATER) { 

     if(s == '=') { 

      return true; 

     } else { 

      return false; 

     } 

    }/*end greater*/ else 

    if(current_state == PLUS) { 

     if(s == '+') { 

      return true; 

     } else { 

      return false; 

     } 

    }/*end plus*/ else 

     if(current_state == MINUS) { 

     if(s == '>') { 

      return true; 

     } else { 

      return false; 

     } 

}/*end minus */ else {  

     return false; 

     } 

} 

state nextState(state current_state, char c) { 

     if(current_state == LESS && c == '-') { 

      return LESSDASH; 

     } else 

      if(c == '<') { 

    return LESS; 

    } else 

    if(c == '>') { 

    return GREATER; 

    } else 

    if(c == '+') { 

    return PLUS; 

    } else 

    if(c == '-') { 

    return MINUS; 

    } else 

    if(isalpha(c) || c == '_') { 

    return ID; 

    } else 

    if(isdigit(c)) { 

    return NUM; 

    } else 

    if(c == '~' || c == '&' || c == '^' || c == '*' || c == '(' || c == ')' || c == '[' || c == ']' || c == ':' || c == ',' || c == '=') { 

     return ICT; 

    } else { 

      return ERR; 

     } 

} 

my_STACK< my_STACK<char> > maxMunch(my_STACK<char> S) { 

    my_STACK<char> P; 

    my_STACK< my_STACK<char> > T; 

    state current_state = FRESH; 

    while(!S.empty()) { 

     while(S.top() == ' ' || S.top() == '\t' || S.top() == '\r' || S.top() == '\n') { 

      S.pop(); 

      if(!P.empty()) { 

       T.push(P); 

       while(!P.empty()) { 

        P.pop(); 

       } 

      } 

      current_state = FRESH; 

     }//end second loop 

     if(canPush(S.top() , current_state)) { 

      P.push(S.top()); 

      current_state = nextState(current_state, S.top()); 

      S.pop(); 

     } else { 

      if(isToken(current_state)/*P is a valid token*/) { 

       T.push(P); 

       newLine(P , current_state); 

      } else { 

       cout << "Tokenizing Error"; 

       newLine(P , current_state); 

      }//end else 

     } 

    }//end loop 


    T.push(P); 


    if(S.empty()) { 

      return T;//returns the tokens 

     } 

} 

/************************************************************************** 
Takes a string s and a stack theStack by refernce. Breaks the stirng s into 
char data types and pushes them into the stack theStack. 
***************************************************************************/ 
void Break_Up_Into_Stack(string s, my_STACK<char> &theStack) 
{ 
    while (!theStack.empty()) {theStack.pop();} // empty the stack before use. 

    for (int pos = s.length() - 1; pos >= 0; pos--) 
     theStack.push(s.at(pos)); 

} 

/************************************************************************** 
Takes a stack of chars s by value. Prints the stack on screen. 
***************************************************************************/ 
void Print_Stack_Of_Char (my_STACK<char> s) 
{ 
    cout << "-----top of stack: -----" << endl; 
    while (!s.empty()) {cout << s.top(); s.pop();} 
    cout << endl; 
    cout << "---Bottom of stack: ----" << endl << endl; 
} 

void printMunch (my_STACK< my_STACK<char>> s) 
{ 

    while (!s.empty()) { 

     Print_Stack_Of_Char (s.top()); 

     cout << "" << endl; 

     s.pop(); 

    } 

} 

void Print_Tokens (my_STACK<my_STACK<char> > s) 
{ 
    int count = 1; 
    Reverse_Stack_Stack(s); 
    while (s.empty() == false) 
{ 
    cout << count << ". "; 
    reverse (s.top()); 
    while (s.top().empty() == false) 
    { 
    cout << s.top().top(); 
    s.top().pop(); 
    } 
    cout << endl; 
    count++; 
    s.pop(); 
    } 
} 

int main() //main function 
{ 
//state A = GREATER; 
//char c = '-'; 
//cout << canPush(c, A) << endl; 
     string inputString; 
    my_STACK<char> charStack; 

    cout << "Enter the string: "; 
    getline(cin, inputString); 

    string testString1 = "83 - 8 * [aVar - (2 + 1)]/aVar2"; 
    string testString2 = "amountOfWork <= 1"; 

    Break_Up_Into_Stack(inputString, charStack); 

    //Print_Stack_Of_Char(charStack); 

    //reverse(charStack); 

    my_STACK <my_STACK<char> > Final; 

    Final = maxMunch(charStack); 

    Print_Tokens(Final); 

    //printMunch(Final); 
    char inp; 
    cin>>inp; 

} 

我不知道什么错误可能是我已经尝试了很多事情,它不起作用。该程序使用原始堆栈,但我需要实现自定义堆栈。

任何帮助,将不胜感激。

感谢

编辑:

以下是编译器输出:

1>------ Build started: Project: project_7, Configuration: Debug Win32 ------ 
1> main.cpp 
1>c:\users\administrator\documents\visual studio 2010\projects\project_7\project_7\generic_stack.h(29): error C2143: syntax error : missing ';' before 'template' 
1>c:\users\administrator\documents\visual studio 2010\projects\project_7\project_7\main.cpp(23): error C2039: 'empty' : is not a member of 'my_STACK<T>' 
1>   with 
1>   [ 
1>    T=char 
1>   ] 
1>c:\users\administrator\documents\visual studio 2010\projects\project_7\project_7\main.cpp(23): fatal error C1903: unable to recover from previous error(s); stopping compilation 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+0

完整的错误是在“输出”窗口中,“错误”窗口只有摘要。既然你没有完整的错误,也没有告诉我们哪一行有错误,我们将不得不做出猜测,我们可能无法帮助你。 – 2012-04-13 17:18:09

+0

'CELL * topCellPtr;'应该是'CELL * topCellPtr;' – 2012-04-13 17:20:00

回答

2
template <class T> 
struct CELL { T element; struct CELL *next; }; 

template <class T> 
class my_STACK 
{ 
private: CELL *topCellPtr; 

您声明CELL作为编译器可以使用的类的模板。重要的是要注意,CELL在技术上并不是一个类。因此你不能有一个指向它的指针。你必须告诉在my_STACK什么课topCellPtr点,所以该行应该是:

private: CELL<T> *topCellPtr; 

你可能不得不在所有的my_STACK的代码做到这一点。

+0

我的屏幕显示你击败了我4秒,但是我输入了更多:P – 2012-04-13 17:21:24

+0

工作正常!非常感谢你! – Dfranc3373 2012-04-13 17:38:14

1

你忘了专门的堆栈类里面你的CELL使用:

private: CELL *topCellPtr; 

shoul d是

private: CELL<T> *topCellPtr; 

这只是一个地方,还有很多其他地方。

相关问题