0

我正在创建一个自定义堆栈以与使用堆栈库的旧类一起使用。

编译时出现以下错误:

错误 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

我的主要功能如下:

#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 ==========
4

2 回答 2

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's 代码中执行此操作。

于 2012-04-13T17:20:18.750 回答
1

您忘记专门CELL在堆栈类中使用:

private:    CELL *topCellPtr;

应该

private:    CELL<T> *topCellPtr;

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

于 2012-04-13T17:20:15.410 回答