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

  • C/C++
  • Thread starter Swamp Thing
  • Start date
  • #1
Swamp Thing
Insights Author
913
586
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
  • #2
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 Swamp Thing
  • #3
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

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
955
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
702
  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
6
Views
899
Back
Top