Why overload prototype statement works in .h file but not in main file

  • Context: C/C++ 
  • Thread starter Thread starter Swamp Thing
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on porting an Arduino C library to a NodeMCU using the Arduino IDE, specifically addressing issues with function overloading and enum conversions. The error "can't convert int to enum, -fpermissive" arises due to the default compiler settings in the Arduino IDE, which are not compatible with the NodeMCU library. The recommended solution is to convert or cast int values to the appropriate enums in the code rather than modifying the library's header or source files, as this approach avoids potential runtime errors and maintains code integrity.

PREREQUISITES
  • Understanding of Arduino IDE and its compiler settings
  • Familiarity with C++ function overloading
  • Knowledge of enum data types in C++
  • Experience with NodeMCU and its libraries
NEXT STEPS
  • Learn about C++ enum casting techniques
  • Research Arduino IDE compiler options and flags
  • Explore best practices for function overloading in C++
  • Investigate the differences between Arduino and NodeMCU programming environments
USEFUL FOR

This discussion is beneficial for developers transitioning Arduino projects to NodeMCU, C++ programmers dealing with enum types, and anyone looking to optimize their use of the Arduino IDE for various hardware platforms.

Swamp Thing
Insights Author
Messages
1,047
Reaction score
775
I am trying to port an Arduino C library onto a NodeMCU using the Arduino IDE. (Edit: actually I am porting a sketch that uses the library, from Arduino to NodeMCU)

There are some functions (members of a certain class) that take enum arguments, but I need to provide int values because the values come in over serial or WiFi. On the Arduino I can just feed ints to the original function and it converts them nicely into the required enums. But when compiling for NodeMCU it raises an error like "can't convert int to enum, -fpermissive".

I am now overloading the function with an additional prototype line in the ".h" file and a modified function definition in the library's ".c" file.

My questions:-

(a) why doesn't it work if I put the new prototype line in my main code just after the library's include line? Why do I have to put it in the library's ".h" file? My understanding was that the compiler just merges all the included contents into the main file before processing. In that case it shouldn't matter if my prototype line followed the include line in my code, or was inside the header file.

(b) Is there some other simple way of overloading the function by writing stuff in my file rather than touching the supplied libraries, keeping in mind that the function is part of a class?
 
Technology news on Phys.org
Swamp Thing said:
I am trying to port an Arduino C library onto a NodeMCU using the Arduino IDE. (Edit: actually I am porting a sketch that uses the library, from Arduino to NodeMCU)
I don't understand what you mean. NodeMCU creates binaries compiled from scripts written in Lua supported by NodeMCU libraries. The Arduino IDE creates binaries compiled from scripts written in C++ supported by Arduino libraries.

My best guess is that you actually mean "I am trying to program a device designed and supplied for use with NodeMCU using the Arduino IDE". In this case NodeMCU is completely irrelevant - when you use the Arduino IDE it replaces everything related to NodeMCU except the name of the board.

Swamp Thing said:
There are some functions (members of a certain class) that take enum arguments, but I need to provide int values because the values come in over serial or WiFi. On the Arduino I can just feed ints to the original function and it converts them nicely into the required enums. But when compiling for NodeMCU it raises an error like "can't convert int to enum, -fpermissive".

The Arduino IDE default compiler settings set the -fpermissive flag: this is a really bad thing, but for backward compatibility reasons it is not going to change. When makers of other boards write their packages for Arduino IDE they can choose what default compiler settings to use, and it seems like whoever wrote the package for the board you are using made the good choice not to set -fpermissive.

You can work round this by making the bad choice yourself and setting -fpermissive in the compiler options: don't do this.

The right (and easiest) way to solve it is to convert or cast the ints into the appropriate enumss in your code (ideally by checking that the values are correct to avoid unexpected behaviour like flames shooting out of the USB port).

Swamp Thing said:
I am now overloading the function with an additional prototype line in the ".h" file and a modified function definition in the library's ".c" file.

Bad idea (it might make flames shoot out of the USB port), and much harder than the correct solution above.

Swamp Thing said:
(a) why doesn't it work if I put the new prototype line in my main code just after the library's include line? Why do I have to put it in the library's ".h" file?
Declarations (most people prefer this term to "prototype" but they mean the same thing here) belong in header files.

Swamp Thing said:
(b) Is there some other simple way of overloading the function by writing stuff in my file rather than touching the supplied libraries, keeping in mind that the function is part of a class?
The right solution is explained above and does not involve overloading anything.
 
  • Like
Likes   Reactions: Swamp Thing
pbuk said:
ideally by checking that the values are correct to avoid unexpected behaviour like flames shooting out of the USB port
I really hate when this happens...
 

Similar threads

Replies
4
Views
5K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
81
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K