Hello,

I'm embarrassed for asking this question but for the life of me I cannot find an answer. This ultra simple code is giving this error as well as any other function that I'm trying to write.

a function-definition is not allowed here before '{' token 
expected `,' or `;' before '{' token

I'm using the latest version of bloodshed to compile 4.9.2.2
Here's the code:

#include <iostream>
#include <string>

int main ()
{
     // return the greatest common divisor
     int gcd(int v1, int v2)
     {
         while (v2) {
             int temp = v2;
             v2 = v1 % v2;
             v1 = temp;
         }
         return v1;
     }
}

Please help me :(

Recommended Answers

All 2 Replies

Hello,

I'm embarrassed for asking this question but for the life of me I cannot find an answer. This ultra simple code is giving this error as well as any other function that I'm trying to write.

a function-definition is not allowed here before '{' token 
expected `,' or `;' before '{' token

I'm using the latest version of bloodshed to compile 4.9.2.2
Here's the code:

#include <iostream>
#include <string>

int main ()
{
     // return the greatest common divisor
     int gcd(int v1, int v2)
     {
         while (v2) {
             int temp = v2;
             v2 = v1 % v2;
             v1 = temp;
         }
         return v1;
     }
}

Please help me :(

You have a nested function. Lines 4 - 16 is the main function. Lines 7 - 15 is the gcd function. It's within the main function. It needs to be before the main function or after the main function. It can't be inside the main function.

#include <iostream>
#include <string>

int main ()
{
     // return the greatest common divisor
     int gcd(int v1, int v2)
     {
         while (v2) {
             int temp = v2;
             v2 = v1 % v2;
             v1 = temp;
         }
         return v1;
     }
}

The program below should compile. It won't do anything since the gcd function is not called, but it's the way you want to lay it out.

#include <iostream>
#include <string>

int gcd(int v1, int v2);

int main ()
{
     return 0;
}


// return the greatest common divisor
int gcd(int v1, int v2)
{
     while (v2) {
         int temp = v2;
         v2 = v1 % v2;
         v1 = temp;
     }
     return v1;
}
commented: Make it count rep. +18

Thank you very much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.