I studied С++ long time and now I need to help a friend. Error and code attached.
Yuzayu cpp.sh :

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

Mistake:

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

  • What is cpp.sh ? - PinkTux
  • Nested functions can be done in gcc (not ++). - avp
  • one
    @avp, habrahabr.ru/post/149513 :-) - PinkTux
  • @PinkTux, well, crosses ... macros from classes ... and in fact at the beginning there was a sensible idea, which was then replaced by a delusion that swept the masses. - avp

1 answer 1

Bring your sqr out of the limits of main - in C ++ you cannot put functions into each other.

 #include <iostream> #include <string> #include <cmath> #include<math.h> 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 << "What is your name? "; getline (std::cin, name); std::cout << "Hello, " << name << "!\n"; std::cout << "KAPEZ"; std::cout << sqr (2, 3); } 
  • one
    Thank you very much!!! HELPED - sergey
  • In fact, you can nest a function definition in another function. There are at least two ways to do this: 1. a member function in a local class. 2. lambda function. - αλεχολυτ
  • @alexolut is not exactly what the author of the question wanted to do. What the author wanted to do (to enclose a named function that is not a class method) cannot be done in C ++. - gbg