Go Down

Topic: Function Definition in Switch Statement (Read 1 time) previous topic - next topic


When I put the following:

Code: [Select]
int calculateSlope(int x){
  int torque = MAX_TORQUE * sqrt(x/(HISTERESIS_SLOPE));
  return torque;

within a case for a switch statement, I get the error 'a function-definition is not allowed here before '{' token';

The code runs fine until I include it as just one 'case' amongst multiple; any ideas?

Thank you very much



You can't nest function definitions anywhere in this flavour of C/C++ (even when they have closing braces)


Functions are complete, standalone entities within a program.  Functions cannot contain other functions, they can only reference them.

Move your function outside your code so it is by itself, and then you should be able to call your function.

Oh, and be sure that your maths is doing what you want - dividing integers by integers doesn't give the same results as dividing floats by floats.  Ensure you're working with sufficiently big numbers.


Ah, thanks everyone, that's sorted. As with many problems, its my fundamental amateurishness with coding which has caused it! I hadn't realised the function was 'outside' the loop.

Thanks for your help!


Go Up