Home c++ Error “a function-definition is not allowed here before '{' token”

Error “a function-definition is not allowed here before ‘{‘ token”

Author

Date

Category

I studied C++ for a long time and now I need to help a friend. Bug and code included.
Yuzayu cpp.sh :

// Example program
#include & lt; iostream & gt;
#include & lt; string & gt;
#include & lt; cmath & gt;
# include & lt; math.h & gt;
using namespace std;
int main ()
{
 / * std :: string name;
 std :: cout & lt; & lt; "What is your name?";
 getline (std :: cin, name);
 std :: cout & lt; & lt; "Hello," & lt; & lt; name & lt; & lt; "! \ n";
 std :: cout & lt; & lt; "KAPEZ"; * /
 float sqr (float fl1, float fl2) {
  // float s = (fl1 * fl2) / 2;
  return (fl1 * fl2) / 2;
 };
 // std :: cout & lt; & lt; sqr (2, 3)
}

Error:

In function ‘int main ()’: 16:35: error: a function-definition is not allowed here before ‘{‘ token


Answer 1, authority 100%

Move your sqr outside of main – you can’t nest functions in C++.

# include & lt; iostream & gt;
#include & lt; string & gt;
#include & lt; cmath & gt;
# include & lt; math.h & gt;
using namespace std;
float sqr (float fl1, float fl2) {
  // float s = (fl1 * fl2) / 2;
  return (fl1 * fl2) / 2;
 };
int main ()
{
 std :: string name;
 std :: cout & lt; & lt; "What is your name?";
 getline (std :: cin, name);
 std :: cout & lt; & lt; "Hello," & lt; & lt; name & lt; & lt; "! \ n";
 std :: cout & lt; & lt; "KAPEZ";
 std :: cout & lt; & lt; sqr (2, 3);
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions