Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. what wrong with this function template?

what wrong with this function template?

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebuggingquestionworkspace
15 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DengJW
    wrote on last edited by
    #1

    #include template T max(T a, T b) { if (a > b) return(a); else return(b); } float max(float, float); int max(int, int); void main(void) { cout << "The maximum of 100 and 200 is " << max(100, 200) << endl; cout << "The maximum of 5.4321 and 1.2345 is " << max(5.4321, 1.2345) << endl; } --------------------Configuration: Max_Temp - Win32 Debug-------------------- Linking... Max_Temp.obj : error LNK2001: unresolved external symbol "int __cdecl max(int,int)" (?max@@YAHHH@Z) Debug/Max_Temp.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. The problem seems with 'int max(int, int);'; after comment this line, no linking error DJ

    J I D H 5 Replies Last reply
    0
    • D DengJW

      #include template T max(T a, T b) { if (a > b) return(a); else return(b); } float max(float, float); int max(int, int); void main(void) { cout << "The maximum of 100 and 200 is " << max(100, 200) << endl; cout << "The maximum of 5.4321 and 1.2345 is " << max(5.4321, 1.2345) << endl; } --------------------Configuration: Max_Temp - Win32 Debug-------------------- Linking... Max_Temp.obj : error LNK2001: unresolved external symbol "int __cdecl max(int,int)" (?max@@YAHHH@Z) Debug/Max_Temp.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. The problem seems with 'int max(int, int);'; after comment this line, no linking error DJ

      J Offline
      J Offline
      jmkhael
      wrote on last edited by
      #2

      Like you said the problem is the redefinition of the max macro as a function When you redefine the max macro as function and you dont supply a code for it the linker would nag about unresolved external(s). either code the max function, or just use the macro (defined in stdlib.h and windef.h)as it expands to #define max(a,b) (((a) > (b)) ? (a) : (b)) Papa while (TRUE) Papa.WillLove ( Bebe ) ;

      1 Reply Last reply
      0
      • D DengJW

        #include template T max(T a, T b) { if (a > b) return(a); else return(b); } float max(float, float); int max(int, int); void main(void) { cout << "The maximum of 100 and 200 is " << max(100, 200) << endl; cout << "The maximum of 5.4321 and 1.2345 is " << max(5.4321, 1.2345) << endl; } --------------------Configuration: Max_Temp - Win32 Debug-------------------- Linking... Max_Temp.obj : error LNK2001: unresolved external symbol "int __cdecl max(int,int)" (?max@@YAHHH@Z) Debug/Max_Temp.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. The problem seems with 'int max(int, int);'; after comment this line, no linking error DJ

        I Offline
        I Offline
        ilostmyid2
        wrote on last edited by
        #3

        sure when u declare: int max(int,int); it looks for a function other than the template function when u use it. when there exists the template function definition, there's no need to explicitly define prototypes. u should remove both prototypes. if u need to use not exactly the same types in max, eg. max(int,float), max(float,int), etc. u may define inline functions like these: float max(int i,float f) { return max(float(i),f); } float max(float f,int i) { return max(f,float(i)); } etc.

        1 Reply Last reply
        0
        • D DengJW

          #include template T max(T a, T b) { if (a > b) return(a); else return(b); } float max(float, float); int max(int, int); void main(void) { cout << "The maximum of 100 and 200 is " << max(100, 200) << endl; cout << "The maximum of 5.4321 and 1.2345 is " << max(5.4321, 1.2345) << endl; } --------------------Configuration: Max_Temp - Win32 Debug-------------------- Linking... Max_Temp.obj : error LNK2001: unresolved external symbol "int __cdecl max(int,int)" (?max@@YAHHH@Z) Debug/Max_Temp.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. The problem seems with 'int max(int, int);'; after comment this line, no linking error DJ

          D Offline
          D Offline
          DengJW
          wrote on last edited by
          #4

          The purpose of this test code is to make use of function template, instead of max funtion, which happened to be here. HOwever, if comment out [int max(int, int);], leave [float max(float, float); ], the code works well. Seems something wrong to the int type? DJ

          I 1 Reply Last reply
          0
          • D DengJW

            #include template T max(T a, T b) { if (a > b) return(a); else return(b); } float max(float, float); int max(int, int); void main(void) { cout << "The maximum of 100 and 200 is " << max(100, 200) << endl; cout << "The maximum of 5.4321 and 1.2345 is " << max(5.4321, 1.2345) << endl; } --------------------Configuration: Max_Temp - Win32 Debug-------------------- Linking... Max_Temp.obj : error LNK2001: unresolved external symbol "int __cdecl max(int,int)" (?max@@YAHHH@Z) Debug/Max_Temp.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. The problem seems with 'int max(int, int);'; after comment this line, no linking error DJ

            H Offline
            H Offline
            Henrik Stuart
            wrote on last edited by
            #5

            There isn't per se anything wrong with the template function, rather with some other parts of the code.

            1. You are using iostream.h, this is a pre-standard header and should not be used. Instead use iostream, without a postfixed .h
            2. You defined two functions manually, float max(float, float); and int max(int, int);. These two functions do not refer to the templated function and will be used before the match to the template function. Hence, you should remove these and the error will go away.
            3. Lastly, have you considered what happens if you do something like this: short s = 37; int a = 2839; int b = max(s, a);? This will fail with an error message, because the compiler isn't able to deduce the type of T (your template parameter). Thus, you need to implement a more solid min/max function, as explained by Andrei Alexandrescu in his Generic< Programming >: Min and Max Redividus[^] article at http://www.cuj.com[^]
            4. Also, this is just a small detail, but you might want to consider using '\n' rather than std::endl, because std::endl also explicitly flushes the stream - can slow down performance unnecessarily (albeit not a lot in most cases).
            5. Lastly, the main signature is according to the standard either int main() or int main(int argc, char* argv[]). void main() is a Microsoft-specific extension, and unless necessary (I haven't yet seen any reason why) it should be avoided for the sake of portability.

            All in all, the code will look like this:

            #include <iostream>

            template<typename T>
            T max(T a, T b) {
            return a > b ? a : b;
            }

            int main() {
            std::cout << "The maximum of 100 and 200 is "
            << max(100, 200) << '\n';

            std::cout << "The maximum of 5.4321 and 1.2345 is "
            << max(5.4321, 1.2345) << '\n';

            return 0;
            }

            I hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^

            D 1 Reply Last reply
            0
            • D DengJW

              The purpose of this test code is to make use of function template, instead of max funtion, which happened to be here. HOwever, if comment out [int max(int, int);], leave [float max(float, float); ], the code works well. Seems something wrong to the int type? DJ

              I Offline
              I Offline
              ilostmyid2
              wrote on last edited by
              #6

              there's no difference between int and float. if u use float too, it nags about it too. the default for numbers including decimal points is double, not float. this is why it didn't error. to test is cast the floating point numbers to float. as i said, u don't need to prototype the existing max template function.

              D 1 Reply Last reply
              0
              • H Henrik Stuart

                There isn't per se anything wrong with the template function, rather with some other parts of the code.

                1. You are using iostream.h, this is a pre-standard header and should not be used. Instead use iostream, without a postfixed .h
                2. You defined two functions manually, float max(float, float); and int max(int, int);. These two functions do not refer to the templated function and will be used before the match to the template function. Hence, you should remove these and the error will go away.
                3. Lastly, have you considered what happens if you do something like this: short s = 37; int a = 2839; int b = max(s, a);? This will fail with an error message, because the compiler isn't able to deduce the type of T (your template parameter). Thus, you need to implement a more solid min/max function, as explained by Andrei Alexandrescu in his Generic< Programming >: Min and Max Redividus[^] article at http://www.cuj.com[^]
                4. Also, this is just a small detail, but you might want to consider using '\n' rather than std::endl, because std::endl also explicitly flushes the stream - can slow down performance unnecessarily (albeit not a lot in most cases).
                5. Lastly, the main signature is according to the standard either int main() or int main(int argc, char* argv[]). void main() is a Microsoft-specific extension, and unless necessary (I haven't yet seen any reason why) it should be avoided for the sake of portability.

                All in all, the code will look like this:

                #include <iostream>

                template<typename T>
                T max(T a, T b) {
                return a > b ? a : b;
                }

                int main() {
                std::cout << "The maximum of 100 and 200 is "
                << max(100, 200) << '\n';

                std::cout << "The maximum of 5.4321 and 1.2345 is "
                << max(5.4321, 1.2345) << '\n';

                return 0;
                }

                I hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^

                D Offline
                D Offline
                DengJW
                wrote on last edited by
                #7

                thanks a lots. now this code works: #include <iostream> template T max(T a, T b) { if (a > b) return(a); else return(b); } //float max(float, float); //int max(int, int); void main(void) { std::cout << "The maximum of 100 and 200 is " << max(100, 200) << '\n'; std::cout << "The maximum of 5.4321 and 1.2345 is " << max(5.4321, 1.2345) << '\n'; } but why the definition of two functions, which have been commented out, not working? BTW, I intended to make use of function template instead of max/min functions. DJ

                H 1 Reply Last reply
                0
                • D DengJW

                  thanks a lots. now this code works: #include <iostream> template T max(T a, T b) { if (a > b) return(a); else return(b); } //float max(float, float); //int max(int, int); void main(void) { std::cout << "The maximum of 100 and 200 is " << max(100, 200) << '\n'; std::cout << "The maximum of 5.4321 and 1.2345 is " << max(5.4321, 1.2345) << '\n'; } but why the definition of two functions, which have been commented out, not working? BTW, I intended to make use of function template instead of max/min functions. DJ

                  H Offline
                  H Offline
                  Henrik Stuart
                  wrote on last edited by
                  #8

                  Read item 2 in my previous post again, it explains why they need to be commented out. In order to explain it a bit further: When you say: int max(int, int); you say there is a function, somewhere, that does this, but not how it does it. When you then type max(100, 200); it tries to use this function, not the template function. This results in it using an undefined function, and that is what it complains about. If you fill out the function, e.g. int max(int a, int b) { return a > b ? a : b; } it would work as well, but it wouldn't, obviously, be using the template function. Please, refer to the rest of my list about the issues in your code. And take a good, long look at Alexandrescu's article and preferably use his min/max functions instead. Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])

                  D 1 Reply Last reply
                  0
                  • I ilostmyid2

                    there's no difference between int and float. if u use float too, it nags about it too. the default for numbers including decimal points is double, not float. this is why it didn't error. to test is cast the floating point numbers to float. as i said, u don't need to prototype the existing max template function.

                    D Offline
                    D Offline
                    DengJW
                    wrote on last edited by
                    #9

                    but if I just comment out int maxf(int, int); , the code works. Moreover, if I comment out float maxf(float, float); , the code works too. DJ

                    I 1 Reply Last reply
                    0
                    • D DengJW

                      but if I just comment out int maxf(int, int); , the code works. Moreover, if I comment out float maxf(float, float); , the code works too. DJ

                      I Offline
                      I Offline
                      ilostmyid2
                      wrote on last edited by
                      #10

                      so comment out both if u like i'm online (invis always) now with id ilostmyid2 at yahoo messenger

                      D 1 Reply Last reply
                      0
                      • H Henrik Stuart

                        Read item 2 in my previous post again, it explains why they need to be commented out. In order to explain it a bit further: When you say: int max(int, int); you say there is a function, somewhere, that does this, but not how it does it. When you then type max(100, 200); it tries to use this function, not the template function. This results in it using an undefined function, and that is what it complains about. If you fill out the function, e.g. int max(int a, int b) { return a > b ? a : b; } it would work as well, but it wouldn't, obviously, be using the template function. Please, refer to the rest of my list about the issues in your code. And take a good, long look at Alexandrescu's article and preferably use his min/max functions instead. Hope this helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])

                        D Offline
                        D Offline
                        DengJW
                        wrote on last edited by
                        #11

                        What I have tried to test was how to use Function Template, then I copied a segment of codes from a book. If I left float TFun(float, float) uncommented, the code works well; however, if I left int TFun(int, int) uncommented, the code did not work. That is the problem I could not figure out.

                        #include
                        template T TFun(T a, T b)
                        {
                        if (a > b)
                        return(a);
                        else
                        return(b);
                        }
                        float TFun(float, float);
                        //int TFun(int, int);
                        void main(void)
                        {
                        std::cout << "The TFunimum of 100 and 200 is " <<
                        TFun(100, 200) << '\n';

                        std::cout << "The TFunimum of 5.4321 and 1.2345 is " << 
                         TFun(5.4321, 1.2345) << '\\n';
                        

                        }

                        DJ

                        H 1 Reply Last reply
                        0
                        • I ilostmyid2

                          so comment out both if u like i'm online (invis always) now with id ilostmyid2 at yahoo messenger

                          D Offline
                          D Offline
                          DengJW
                          wrote on last edited by
                          #12

                          thanks. my concern is why int max(int, int) not working but float max(float, float) working. What I had tested was funtion templates, not max/min funtion. So I renamed the function name to confuse less. The following codes work:

                          #include
                          template T TFun(T a, T b)
                          {
                          if (a > b)
                          return(a);
                          else
                          return(b);
                          }
                          float TFun(float, float);
                          //int TFun(int, int);
                          void main(void)
                          {
                          std::cout << "The TFunimum of 100 and 200 is " <<
                          TFun(100, 200) << '\n';
                          std::cout << "The TFunimum of 5.4321 and 1.2345 is " <<
                          TFun(5.4321, 1.2345) << '\n';
                          }

                          DJ

                          I 1 Reply Last reply
                          0
                          • D DengJW

                            thanks. my concern is why int max(int, int) not working but float max(float, float) working. What I had tested was funtion templates, not max/min funtion. So I renamed the function name to confuse less. The following codes work:

                            #include
                            template T TFun(T a, T b)
                            {
                            if (a > b)
                            return(a);
                            else
                            return(b);
                            }
                            float TFun(float, float);
                            //int TFun(int, int);
                            void main(void)
                            {
                            std::cout << "The TFunimum of 100 and 200 is " <<
                            TFun(100, 200) << '\n';
                            std::cout << "The TFunimum of 5.4321 and 1.2345 is " <<
                            TFun(5.4321, 1.2345) << '\n';
                            }

                            DJ

                            I Offline
                            I Offline
                            ilostmyid2
                            wrote on last edited by
                            #13

                            i answered ur question before as i said TFun(5.4321, 1.2345) maps to double TFun(double,double), not float TFun(float,float). ie. u didn't use float. this is why it's ok. if u declare a prototype as: double TFun(double,double); it errors

                            1 Reply Last reply
                            0
                            • D DengJW

                              What I have tried to test was how to use Function Template, then I copied a segment of codes from a book. If I left float TFun(float, float) uncommented, the code works well; however, if I left int TFun(int, int) uncommented, the code did not work. That is the problem I could not figure out.

                              #include
                              template T TFun(T a, T b)
                              {
                              if (a > b)
                              return(a);
                              else
                              return(b);
                              }
                              float TFun(float, float);
                              //int TFun(int, int);
                              void main(void)
                              {
                              std::cout << "The TFunimum of 100 and 200 is " <<
                              TFun(100, 200) << '\n';

                              std::cout << "The TFunimum of 5.4321 and 1.2345 is " << 
                               TFun(5.4321, 1.2345) << '\\n';
                              

                              }

                              DJ

                              H Offline
                              H Offline
                              Henrik Stuart
                              wrote on last edited by
                              #14

                              As ilostmyid2 pointed out then TFun(5.4321, 1.2345) calls the template function with T = double, if you want to use the float TFun(float, float); function instead you should call it with TFun(5.4321f, 1.2345f);. In other words, you are trying to match the wrong types together, and naturally the template function will be chosen as the primitive float function is never being picked by your code. Hope that should clarify it. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])

                              1 Reply Last reply
                              0
                              • D DengJW

                                #include template T max(T a, T b) { if (a > b) return(a); else return(b); } float max(float, float); int max(int, int); void main(void) { cout << "The maximum of 100 and 200 is " << max(100, 200) << endl; cout << "The maximum of 5.4321 and 1.2345 is " << max(5.4321, 1.2345) << endl; } --------------------Configuration: Max_Temp - Win32 Debug-------------------- Linking... Max_Temp.obj : error LNK2001: unresolved external symbol "int __cdecl max(int,int)" (?max@@YAHHH@Z) Debug/Max_Temp.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. The problem seems with 'int max(int, int);'; after comment this line, no linking error DJ

                                D Offline
                                D Offline
                                DengJW
                                wrote on last edited by
                                #15

                                thanks for your quick reply. DJ

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups