2015-08-19 58 views
1
void main1()  
{ 

    const int MAX = 50; 

    class infix 
    { 

    private: 

      char target[MAX], stack[MAX]; 
      char *s, *t; 
      int top, l; 
     public: 
      infix(); 
      void setexpr (char *str); 
      void push (char c); 
      char pop(); 
      void convert(); 
      int priority (char c); 
      void show(); 
    }; 
    void infix :: infix() //error 
    { 
     top = -1; 
     strcpy (target, ""); 
     strcpy (stack, ""); 
     l = 0; 
    } 
    void infix :: setexpr (char *str)//error 
    { 
     s = str; 
     strrev (s); 
     l = strlen (s); 
     * (target + l) = '\0'; 
     t = target + (l - 1); 
    } 
    void infix :: push (char c)//error 
    { 
     if (top == MAX - 1) 
      cout << "\nStack is full\n"; 
     else 
     { 
      top++ ; 
      stack[top] = c; 
     } 
    } 
} 

我有這個代碼有問題。這是我的中間代碼前綴轉換器的一部分。我的編譯器不斷給我錯誤:錯誤功能定義不允許在這裏之前'{'令牌

"A function-declaration is not allowed here – before '{' token"

這個項目實際上有三個錯誤。我的項目將於2015年9月到期,請大家幫忙!提前致謝。

+3

移動類和它的功能的外main1函數。 –

回答

0

您錯過了主要功能的關閉}

+0

這個問題肯定是錯字。 –

+0

@AneeshDandime - 社區解決了你的問題! –

3

您的main函數中有您的類的函數定義,這是不允許的。爲了解決這個問題,你應該外面放置它們,但要做到這一點,你需要將整個班級的地方主要外以及(因爲你需要它的範圍):

class A 
{ 
public: 
    void foo(); 
}; 

void A::foo() 
{ 
    <...> 
} 

int main() 
{ 
    <...> 
} 

值得一提的是,雖然可以把整個類定義你的主,這是不是最好的方法:

int main() 
{ 
    class A 
    { 
    public: 
     void foo() 
     { 
      <...> 
     } 
    } 
} 
+0

我在哪裏放置const int MAX = 50; – Clones

+0

@克隆人,如果你只在課堂上使用它,你應該讓它成爲班級成員。 – SingerOfTheFall

+0

新的錯誤發生在我身上。 「top,target和strcpy沒有在此範圍內聲明」 它說 – Clones

0

U可以使用下面的代碼

#include <iostream> 
#include <string.h> 
using namespace std; 

const int MAX = 50 ; 

class infix 
{ 

private : 

     char target[MAX], stack[MAX] ; 
     char *s, *t ; 
     int top, l ; 
    public : 
     infix() ; 
     void setexpr (char *str) ; 
     void push (char c) ; 
     char pop() ; 
     void convert() ; 
     int priority (char c) ; 
     void show() ; 
}; 
infix::infix() //error 
{ 
    top = -1 ; 
    strcpy (target, "") ; 
    strcpy (stack, "") ; 
    l = 0 ; 
} 
void infix :: setexpr (char *str)//error 
{ 
    s = str ; 
// strrev (s) ; 
    l = strlen (s) ; 
    * (target + l) = '\0' ; 
    t = target + (l - 1) ; 
} 
void infix :: push (char c)//error 
{ 
    if (top == MAX - 1) 
     cout << "\nStack is full\n" ; 
    else 
    { 
     top++ ; 
     stack[top] = c ; 
    } 
} 

int main() 
{ 
    /* call ur function from here*/ 
} 
相關問題