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.

主编

#include<iostream>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "页: 1"


#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("
STACK 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("
%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 ==  	 )
    {
        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 =   ;
}

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
",&post[0]);

return 0;
}

页: 1

#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);




};

如果能帮助,我会利用视频演播室。

EDIT: added comment for clarity.

Thanks, Ryan

最佳回答

function:

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

(:)

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

我不敢肯定,既然“试图称职”是模糊的。 你可能希望准确缩小错误发生之处,然后用补充资料来回答你的问题。


Edit: In looking at your implementation a bit more, I m not sure why you created the mystack class at all. 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. 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. Something of the nature:

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

虽然我不敢肯定你为什么要这样做。 另一种选择是滚动stack。 相反,通过将整部结构作为该类成员,或者简单地将<条码>、<条码>和<条码>至<条码>改为该类。 接着,如果您上下了mystack。 它的标本将掌握 st的数据,并能够用自己的数据使用方法。 我也建议研究与C++课程及其使用相关的辅导/记录/手册。 这里是,但无疑还有大量其他人。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签