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
908
572
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...
 

1. Why should I define overloaded prototypes in a header file rather than directly in the main file?

Defining overloaded prototypes in a header file ensures better modularity and reusability of your code. Header files are designed to be shared and reused across different parts of a program or even different programs. By placing the function prototypes in a header file, you allow any source file that includes the header to know about these functions and use them, thus promoting code reusability and reducing redundancy.

2. What happens if I declare overloaded function prototypes in the main file?

Declaring overloaded function prototypes directly in the main file limits their scope to only that file. Other source files in your project will not be aware of these functions unless they are also declared there, which can lead to code duplication and a higher chance of errors. Furthermore, if the implementation of these functions is in a different file, you will encounter linking errors, as the linker will not be able to resolve references to these functions from other files.

3. Can overloaded function prototypes be declared in both the header and main files?

Yes, overloaded function prototypes can technically be declared in both the header and the main files, but this practice should be avoided to prevent confusion and potential conflicts. If you declare a prototype in a header file, ensure it is not redeclared in the main file unless necessary for specific scenarios like conditional compilation. Consistent use of header files for declarations provides clarity and maintains the integrity of the codebase.

4. Are there any specific rules for defining overloaded functions in C++?

In C++, overloaded functions must differ in the number or type of their parameters. They cannot differ solely by return type. When defining overloaded functions, it is crucial to ensure that each version of the function has a unique signature so that the compiler can correctly resolve which function to call based on the arguments provided. Properly declaring these in a header file ensures that all potential ambiguities are handled before compiling the main parts of the program.

5. What are the common mistakes to avoid when working with overloaded functions?

Common mistakes include not providing enough difference in parameter types or numbers between the overloaded functions, which can lead to ambiguity errors during compilation. Another error is failing to declare all versions of the overloaded functions in the same scope, which can lead to unresolved external symbol errors during linking. Additionally, inconsistent declaration and definition (i.e., declaring a function in a header file but providing a different signature in the implementation) can also lead to compilation errors.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
922
  • 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
637
  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
Replies
6
Views
889
Back
Top