In C++ is there a maximum number of functions that can overload each other?
I’m getting perplexing “no matching function” errors. I have an overloaded encode function with about 900 different versions for each struct/value type that I’m encoding (including templates for arrays/vectors). The encode functions call each other depending on which fields need to be encoded.
If I put my encode(std::string) function at the bottom of the files (.h & .cpp) the calls to it return “no matching function”. If it’s near the top then they don’t.
What’s going on and, more importantly, how should I fix it?
(linux gcc version 4.6.1)
By your description of the error appearing and disappearing based on where you put the function, this has nothing to do with the number of overloads. Rather, the problem is that you’re trying to call an overload before its declared.
When you have a bunch of functions that all call each other like you describe (regardless of whether they are overloads or have distinct names), you generally need to first DECLARE all the functions and then DEFINE them later after all the definitions have been seen. Usually you structure this so the declarations are all in a header file (so other files and include it) and the definitions are all in a source file (which includes the header), so this all works easily. Things get more complex if you have inline functions (which generally need to be defined in the header file to be inlined in all compilation units), but the overall pattern is the same — first DECLARE everything, and then DEFINE everything.
edit
where exactly are you getting the ‘no matching overload’ error? — you need to move the DEFINITION containing the call that is getting that error AFTER the DECLARATION of the encode function (or move the encode DECLARATION before the DEFINITION with the error). As long as ALL DECLARATIONS are before ALL DEFINITIONS, the ordering will be ok.