简体   繁体   English

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

[英]c++ referencing class functions error: identifier is undefined

I am trying to call a function for my stack class. 我正在尝试为我的堆栈类调用一个函数。 If I have all of the functions within the main file the project works, however, when called from the class it says the the Error: identifier "function name" is undefined. 如果我在主文件中具有项目工作的所有函数,但是,当从类调用它时,它表示错误:标识符“函数名称”未定义。 I think it is a simple syntax error, but i can't find it. 我认为这是一个简单的语法错误,但我找不到它。

main.cpp 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 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);




};

I am using visual studio if that helps. 如果有帮助,我正在使用visual studio。

EDIT: added comment for clarity. 编辑:为清晰起见添加评论。

Thanks, Ryan 谢谢,瑞恩

At a cursory glance, this function: 粗略地看一下,这个功能:

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

Is missing the scope operator ( :: ), so you probably intended to write it as: 缺少范围运算符( :: ,因此您可能打算将其写为:

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

I'm not sure if that's your problem though, since "I'm trying to call a function" is a bit vague. 我不确定这是不是你的问题,因为“我试图调用一个函数”有点模糊。 You might want to narrow down precisely where the error is occurring, then edit your question with additional information. 您可能希望精确缩小发生错误的位置,然后使用其他信息编辑您的问题。


Edit: In looking at your implementation a bit more, I'm not sure why you created the mystack class at all. 编辑:在更多地查看您的实现时,我不确定您为什么要创建mystack类。 It looks like you just want to define a bunch of functions that operate on your stack struct, which doesn't require a class definition. 看起来你只想定义一堆在你的stack结构上运行的函数,它不需要类定义。 If you want to do it this way for some unusual reason, then you'll have to instantiate a mystack object before you can call its member functions. 如果你想以某种不寻常的方式这样做,那么你必须在调用其成员函数之前实例化一个mystack对象。 Something of the nature: 性质的东西:

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

Though I'm not sure why you would want to do this. 虽然我不确定你为什么要这样做。 The other alternative is to roll your stack struct into the class instead, either by making the whole struct a member of the class or by simply adding data and top to the class. 另一种方法是滚动您stack代替,或者通过使整个结构的类的成员或通过简单地添加到结构类datatop的类。 Then if you instantiated a mystack object it would have the data of the stack and could call methods on its own data. 然后,如果你实例化一个mystack对象,它将拥有堆栈的数据,并可以调用自己的数据上的方法。 I'd also suggest looking at a tutorial/documentation/book related to C++ classes and their usage. 我还建议查看与C ++类及其用法相关的教程/文档/书籍。 Here's one , but there are undoubtedly plenty of others. 这是一个 ,但毫无疑问还有很多其他的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM