Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/How to avoid the error "error: a function-definition is not allowed here before '{' token"?
Next
Answered
June O'Connor
  • 34
June O'Connor
Asked: May 17, 20222022-05-17T13:56:34+00:00 2022-05-17T13:56:34+00:00In: cpp

How to avoid the error “error: a function-definition is not allowed here before ‘{‘ token”?

  • 34

. Advertisement .

..3..

. Advertisement .

..4..

I have the following cpp code, but I do not know how to find the correct result. Why has this problem occurred, and how can it be solved?
Here is the code that I am running:

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;
  }
  }
 }

And this is the error text I receive:

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

a function-definition is not allowed
  • 2 2 Answers
  • 1k Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-06T02:25:20+00:00Added an answer on June 6, 2022 at 2:25 am

    The cause: This error happens because of these following reasons:

    1. There is not a closing } in the main function.
    2. It is not allowed to put classes’ function definitions inside your main function.

    Solution: To solve this error, you need to classes’ function definitions and total classes outside of main (because it need to be in bound). Therefore, your program should be written as below:

    class A
    {
    public:
    void foo();
    };
    
    void A::foo()
    {
    <...>
    }
    
    int main()
    {
    <...>
    }
    • 19
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Aaron Doucet
    2022-05-25T20:02:58+00:00Added an answer on May 25, 2022 at 8:02 pm

    Your main function has your classes’ function definitions in it, which is illegal. You can fix this by placing them outside. However, the entire class must be placed outside of main (since it is necessary to be within scope).

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

    While it is possible to include the entire class definition in your main, this is not the best way.

    int main()
    {
     class A
     {
     public:
     void foo()
     {
     <...>
     }
     }
    }
    • 14
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.