C++ 错误,此处不允许在“{”标记之前定义函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32088486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:05:15  来源:igfitidea点击:

Error a function-definition is not allowed here before '{' token

c++

提问by Clones

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 ) = '
class A
{
public:
    void foo();
};

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

int main()
{
    <...>
}
'; t = target + ( l - 1 ); } void infix :: push ( char c )//error { if ( top == MAX - 1 ) cout << "\nStack is full\n"; else { top++ ; stack[top] = c; } } }

I am having trouble with this code. This is a part of my code for infix to prefix converter. My compiler keeps giving me the error:

我在使用此代码时遇到问题。这是我的中缀到前缀转换器代码的一部分。我的编译器不断给我错误:

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

“此处不允许使用函数声明 - 在 '{' 标记之前”

There's actually three errors in this project. My project is due in September 2015, so please help! Thanks in advance.

这个项目实际上存在三个错误。我的项目将于 2015 年 9 月到期,所以请帮忙!提前致谢。

回答by SingerOfTheFall

You have your classes' function definitions inside your mainfunction, which is not allowed. To fix that, you should place them outside, but to do that you will need to place the whole class outside of main as well (since you need it to be in scope):

您的函数中有类的函数定义main,这是不允许的。要解决这个问题,您应该将它们放在外面,但要做到这一点,您还需要将整个类放在 main 之外(因为您需要它在范围内):

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

It's worth noting that, while it is possible to put the whole class definition inside your main, this is not the best approach:

值得注意的是,虽然可以将整个类定义放在 main 中,但这不是最好的方法:

 #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 ) = '##代码##' ;
    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 your function from here*/
}

回答by J?rgen

You are missing a closing }for the main function.

您缺少}main 函数的结尾。

回答by Sagar Patel

You can use the code below:

您可以使用以下代码:

##代码##