2010-11-11 75 views
3

我想爲我的堆棧類調用一個函數。如果我擁有主文件中的所有函數,那麼項目就可以工作,但是,當從類中調用時,它說錯誤:標識符「函數名稱」未定義。我認爲這是一個簡單的語法錯誤,但我找不到它。C++引用類函數錯誤:標識符未定義

的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是否有幫助。

編輯:添加評論爲清晰。

感謝, 瑞安

+1

爲什麼你有一個僅使用其他類的成員變量的成員函數的類? – 2010-11-11 04:12:21

+0

我不明白你的意思? – Ryan 2010-11-11 04:20:31

回答

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++類及其用法相關的教程/文檔/書籍。 Here's one,但毫無疑問有很多其他人。

相關問題