C++引用类函数错误:标识符未定义。

3
我正在尝试调用我的堆栈类的函数。如果我在主文件中拥有所有的函数,项目就可以工作,但是当从类中调用时,它会说错误:标识符“函数名称”未定义。我认为这是一个简单的语法错误,但我找不到它。
main.cpp
#include<iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "stack.h"


#define MAX 10
#define EMPTY -1

struct stack
{
char data[MAX];
int top;
 };


int mystack::isempty(struct stack *s)
{
return (s->top == EMPTY) ? 1 : 0;
}

void mystack::emptystack(struct stack* s)
{
s->top=EMPTY;
}

void mystack::push(struct stack* s,int item)
{
if(s->top == (MAX-1))
{
    printf("\nSTACK FULL");
}
else
{
    ++s->top;
    s->data[s->top]=item;
}
}

char mystack::pop(struct stack* s)
{
char ret=(char)EMPTY;
if(!isempty(s))
{
    ret= s->data[s->top];
    --s->top;
}
return ret;
}

void mystack::display(struct stack s)
{
while(s.top != EMPTY)
{
    printf("\n%d",s.data[s.top]);
    s.top--;
}
}



int isoperator(char e)
{
if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%' || e == '^')
    return 1;
else
    return 0;
}


int priority(char e)
{
int pri = 0;
if(e =='%' || e == '^')
    pri = 3;
else
{
    if (e == '*' || e == '/' || e =='%')
    pri = 2;
    else
    {
    if(e == '+' || e == '-')
        pri = 1;
    }
}

return pri;
}

void infix2postfix(char* infix, char * postfix, int insertspace)
{
char *i,*p;
struct stack X;
char n1;
emptystack(&X); // any time a class like this is called it says Error: identifier "emptystack"
                                                             // is undefined
i = &infix[0];
p = &postfix[0];

while(*i)
{
    while(*i == ' ' || *i == '\t')
    {
        i++;
    }

    if( isdigit(*i) || isalpha(*i) )
    {
        while( isdigit(*i) || isalpha(*i))
        {
            *p = *i;
            p++;
            i++;
        }

        if(insertspace)
        {
            *p = ' ';
            p++;
        }

    }

    if( *i == '(' )
    {
        push(&X,*i);
        i++;
    }

    if( *i == ')')
    {
        n1 = pop(&X);
        while( n1 != '(' )
        {
            *p = n1;
            p++;

            if(insertspace)
            {
                *p = ' ';
                p++;
            }

            n1 = pop(&X);
        }
        i++;
    }

    if( isoperator(*i) )
    {
        if(mystack::isempty(&X))
            push(&X,*i);
        else
        {
            n1 = pop(&X);
            while(priority(n1) >= priority(*i))
            {
                *p = n1;
                p++;

                if(insertspace)
                {
                    *p = ' ';
                    p++;
                }

                n1 = pop(&X);
            }
            push(&X,n1);
            push(&X,*i);
        }
        i++;
    }
}
while(!isempty(&X))
{
    n1 = pop(&X);
    *p = n1;
    p++;

    if(insertspace)
    {
        *p = ' ';
        p++;
    }

}
*p = '\0';
}

int main()
{
char in[50],post[50];

strcpy(&post[0],"");
printf("Enter Infix Expression : ");
fflush(stdin);
gets(in);
infix2postfix(&in[0],&post[0],1);
printf("Postfix Expression is : %s\n",&post[0]);

return 0;
}

stack.h

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

using namespace std;

class mystack
{
public:


int isempty(struct stack *s);
void emptystack(struct stack* s);
void push(struct stack* s,int item);
char pop(struct stack* s);
void display(struct stack s);




};

我正在使用Visual Studio,如果这有所帮助。

编辑:为了更加清晰,添加了注释。

谢谢, Ryan


1
为什么你有一个类,其中成员函数只操作另一个类的成员变量? - James McNellis
我不明白你的意思? - Ryan
1个回答

11

乍一看,这个函数:

void emptystack(struct stack* s)
{
    s->top=EMPTY;
}

缺少作用域操作符 (::),所以您可能打算将其写成:

void mystack::emptystack(struct stack* s)
{
    s->top=EMPTY;
}

我不确定这是否是您的问题,因为“我正在尝试调用一个函数”有点含糊不清。您可能需要缩小发生错误的精确位置,然后使用附加信息编辑您的问题。


编辑:在更详细地查看您的实现时,我不确定为什么您完全要创建mystack类。看起来您只想定义一堆操作stack结构的函数,这不需要一个类定义。如果您出于某些不寻常的原因想以这种方式进行操作,那么您必须在调用其成员函数之前实例化一个mystack对象。如下所示:

mystack * myStackObj = new mystack();
myStackObj->emptystack(&X);

虽然我不确定您为什么要这样做。另一种选择是将您的stack结构体合并到类中,可以通过将整个结构体作为类成员或仅添加datatop到类中实现。如果您实例化了一个mystack对象,它将具有堆栈数据并且可以调用其自身数据的方法。我还建议查看与C++类及其用法相关的教程/文档/书籍。这里有一个,但无疑还有其他很多。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接