views:

210

answers:

3

Hello,

I want to use this code (from my last question (thanks Adam)),

bool AllDigitsIdentical(int number)
{
    int lastDigit = number % 10;
    number /= 10;
    while(number > 0)
    {
        int digit = number % 10;
        if(digit != lastDigit)
            return false;
        number /= 10;
    }

    return true;
}

but the compiler just says in the second line at } :

Nested functions are disabled, use -fnested-functions to re-enable

What can I do in my case? I have no plan…

Thanks and sorry for my bad English.

A: 

this function looks fine, is it possible you missed a closing } in the code before this function ?

Alon
I´m sure that I didnt missed a closing } before the function.
Flocked
A: 

You probably pasted that into a method or into your main function! Make sure that that chuck of code is put OUTSIDE any other blocks of code.

so if you have main here:

int main(int argc, char **argv){
     //blah
}

make sure that you put that code above or below it like so:

bool AllDigitsIdentical(int number){
    //blah
}

do NOT put it inbetween the { or } of the main function (or any other method)

micmoo
No I didnt put the code into a function.
Flocked
Then why did you accept the answer that said you did!
micmoo
+1  A: 

You wouldn't happen to have something like:

- (void) someMethod
{

    bool AllDigitsIdentical(int number)
    {
        int lastDigit = number % 10;
        number /= 10;
        while(number > 0)
        {
            int digit = number % 10;
            if(digit != lastDigit)
                return false;
            number /= 10;
        }

        return true;
    }

}

That is, you have a function declared within a method's scope of implementation (though the same problem would occur for a function declared within a function).

In short, don't do that. It isn't supported and the means via which GCC implements it is considered to be a bit of a security hole (IIRC).

Move it outside:

 bool AllDigitsIdentical(int number)
{
    int lastDigit = number % 10;
    number /= 10;
    while(number > 0)
    {
        int digit = number % 10;
        if(digit != lastDigit)
            return false;
        number /= 10;
     }

     return true;
}


- (void) someMethod
{

    .... call the function here ....
}
bbum