typdef has to be before any function declaration?

By experimenting it seems to me that I have to do a typedef before declaring any functions. Am I right?

This compiles:

typedef enum {
    OnePeak,
    TwoPeak,
    ThreePeak,
    Interpolated
} TrainType_e ;

double Stress (const double x)
{
    double y = 1.0/(1.0 + exp(-x)) ; 
    return y ;
}

This does not:

double Stress (const double x)
{
    double y = 1.0/(1.0 + exp(-x)) ; 
    return y ;
}

typedef enum {
    OnePeak,
    TwoPeak,
    ThreePeak,
    Interpolated
} TrainType_e ;

Or have I missed something?

tydeff ,declaring variableS only at the begin of a code.

This does not:

Please post a full sketch that illustrates the problem

Arduino is programmed in C++, not C. C++ doesn't use typedef for enums. Use enum TrainType { OnePeak, ... };, or even better use a scoped enum, using enum class TrainType { ... };.

See Enumeration declaration - cppreference.com.

Pieter

Pieter, that does not change the problem.

This compete program does not compile

double Test (const double x)
{
    double y = 1.0/(1.0 + exp(-x)) ; 
    return y ;
}

enum class TrainType_e {
    OnePeak,
    TwoPeak,
    ThreePeak,
    Interpolated
} ;

void setup()
{
}

TrainType_e GetRandomTrainType () 
{
    int iType = random (int(TrainType_e::OnePeak),int(TrainType_e::Interpolated)+1) ;
    return TrainType_e(iType) ;      
}

void loop()
{
    TrainType_e a = GetRandomTrainType () ; 
    while(1);
}

This one does.

enum class TrainType_e {
    OnePeak,
    TwoPeak,
    ThreePeak,
    Interpolated
} ;

double Test (const double x)
{
    double y = 1.0/(1.0 + exp(-x)) ; 
    return y ;
}

void setup()
{
}

TrainType_e GetRandomTrainType () 
{
    int iType = random (int(TrainType_e::OnePeak),int(TrainType_e::Interpolated)+1) ;
    return TrainType_e(iType) ;      
}

void loop()
{
    TrainType_e a = GetRandomTrainType () ; 
    while(1);
}

And I'm wondering what I am missing...

woundr:
tydeff ,declaring variableS only at the begin of a code.

So it it not like C++ where you can declare variables and types anywhere?

At the start of compilation, the Arduino IDE makes some small changes to your .ino files to ensure it's valid C++. It will add function prototypes for any function that doesn't already have one. This is intended to make it easier for beginners to write code in the Arduino IDE, and also automates a tedious task. After inserting the function prototypes, the IDE adds #line directives so that error/warning messages will still match your sketch. In some rare cases, the Arduino IDE does not take the correct action during function prototype generation. This can lead to very confusing errors. Sometimes the error messages don't even match the code in your sketch, since the error line is the "hidden" code inserted by the Arduino IDE. When you encounter such an error, it can be very helpful to examine the post-sketch preprocessing output:

  • File > Preferences
  • Check the box next to "Show verbose output during: compilation'
  • Click "OK"
  • Sketch > Verify/Compile
  • After compilation fails, scroll the black console window at the bottom of the Arduino IDE window all the way to the top.
  • Examine the first line of output to find the value of the "-build-path" option.

For example, if you had this:

C:\ArduinoIDE\arduino-nightly\arduino-builder -dump-prefs -logger=machine -hardware C:\ArduinoIDE\arduino-nightly\hardware -hardware C:\Users\per\AppData\Local\Arduino15\packages -hardware E:\electronics\arduino\hardware -tools C:\ArduinoIDE\arduino-nightly\tools-builder -tools C:\ArduinoIDE\arduino-nightly\hardware\tools\avr -tools C:\Users\per\AppData\Local\Arduino15\packages -built-in-libraries C:\ArduinoIDE\arduino-nightly\libraries -libraries E:\electronics\arduino\libraries -fqbn=esp32:esp32:node32s:FlashFreq=80,UploadSpeed=921600 -vid-pid=0X2341_0X0042 -ide-version=10809 -build-path C:\Users\per\AppData\Local\Temp\arduino_build_889992 -warnings=all -build-cache C:\Users\per\AppData\Local\Temp\arduino_cache_764477 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.esptool.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\esptool\2.3.1 -prefs=runtime.tools.esptool-2.3.1.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\esptool\2.3.1 -prefs=runtime.tools.mkspiffs.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\mkspiffs\0.2.3 -prefs=runtime.tools.mkspiffs-0.2.3.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\mkspiffs\0.2.3 -prefs=runtime.tools.xtensa-esp32-elf-gcc.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0 -prefs=runtime.tools.xtensa-esp32-elf-gcc-1.22.0-80-g6c4433a-5.2.0.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0 -verbose C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_19197\sketch_dec31a.ino

the temporary build folder is C:\Users\per\AppData\Local\Temp\arduino_build_889992.

Open the .ino.cpp file in the sketch subfolder of the temporary build folder with a text editor.

The temporary build folder will be deleted when you exit the Arduino IDE.


In this case, the code after sketch preprocessing looks like this:

#include <Arduino.h>
#line 1 "E:\\electronics\\arduino\\temp\\685955\\685955.ino"


#line 3 "E:\\electronics\\arduino\\temp\\685955\\685955.ino"
double Test(const double x);
#line 16 "E:\\electronics\\arduino\\temp\\685955\\685955.ino"
void setup();
#line 20 "E:\\electronics\\arduino\\temp\\685955\\685955.ino"
TrainType_e GetRandomTrainType();
#line 26 "E:\\electronics\\arduino\\temp\\685955\\685955.ino"
void loop();
#line 3 "E:\\electronics\\arduino\\temp\\685955\\685955.ino"
double Test (const double x)
{
    double y = 1.0/(1.0 + exp(-x)) ;
    return y ;
}

enum class TrainType_e {
    OnePeak,
    TwoPeak,
    ThreePeak,
    Interpolated
} ;

void setup()
{
}

TrainType_e GetRandomTrainType ()
{
    int iType = random (int(TrainType_e::OnePeak),int(TrainType_e::Interpolated)+1) ;
    return TrainType_e(iType) ;     
}

void loop()
{
    TrainType_e a = GetRandomTrainType () ;
    while(1);
}

You can see the problem is the function prototype for GetRandomTrainType() is being inserted above the declaration of the TrainType_e type. The Arduino sketch preproccessor only generates function prototypes for the functions that don't already have a prototype, so you can fix this sort of issue by adding a prototype at the correct location in the code (after the declaration of the TrainType_e type:

TrainType_e GetRandomTrainType ();

giovanniguerra:
So it it not like C++ where you can declare variables and types anywhere?

Yes and no. The compiler is regular C++. But, the Arduino IDE does some "helpful" things that in this case is messing you up due to auto-prototype generation. Your code compiles fine in Eclipse / Sloeber. In Arduino IDE, you'd need to add the function prototype:

double Test (const double x) {
  double y = 1.0 / (1.0 + exp(-x)) ;
  return y ;
}

enum class TrainType_e {
  OnePeak,
  TwoPeak,
  ThreePeak,
  Interpolated
};

TrainType_e GetRandomTrainType();

void setup() {
}

TrainType_e GetRandomTrainType() {
  int iType = random (int(TrainType_e::OnePeak), int(TrainType_e::Interpolated) + 1) ;
  return TrainType_e(iType) ;
}

void loop() {
  TrainType_e a = GetRandomTrainType () ;
  while (1);
}

Thanks to gfvalvo and pert. I thought I was going mad, but now understand. Thanks again!

You're welcome. I'm glad if I was able to be of assistance. Enjoy!
Per