Click here to Skip to main content
15,918,742 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: what wrong with this function template? Pin
ilostmyid225-May-04 2:39
professionalilostmyid225-May-04 2:39 
AnswerRe: what wrong with this function template? Pin
DengJW25-May-04 2:47
DengJW25-May-04 2:47 
GeneralRe: what wrong with this function template? Pin
ilostmyid225-May-04 2:51
professionalilostmyid225-May-04 2:51 
GeneralRe: what wrong with this function template? Pin
DengJW25-May-04 3:14
DengJW25-May-04 3:14 
GeneralRe: what wrong with this function template? Pin
ilostmyid225-May-04 3:25
professionalilostmyid225-May-04 3:25 
GeneralRe: what wrong with this function template? Pin
DengJW25-May-04 3:36
DengJW25-May-04 3:36 
GeneralRe: what wrong with this function template? Pin
ilostmyid225-May-04 3:43
professionalilostmyid225-May-04 3:43 
AnswerRe: what wrong with this function template? Pin
Henrik Stuart25-May-04 2:49
Henrik Stuart25-May-04 2:49 
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>
<br />
template<typename T>
T max(T a, T b) {
  return a > b ? a : b;
}
<br />
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/[^])
GeneralRe: what wrong with this function template? Pin
DengJW25-May-04 3:06
DengJW25-May-04 3:06 
GeneralRe: what wrong with this function template? Pin
Henrik Stuart25-May-04 3:14
Henrik Stuart25-May-04 3:14 
GeneralRe: what wrong with this function template? Pin
DengJW25-May-04 3:31
DengJW25-May-04 3:31 
GeneralRe: what wrong with this function template? Pin
Henrik Stuart25-May-04 3:54
Henrik Stuart25-May-04 3:54 
AnswerI understand now. thanks,.. ilostmyid2 , henrik and papa Pin
DengJW25-May-04 4:20
DengJW25-May-04 4:20 
Questionhow to create temporary canvas Pin
Arun AC25-May-04 2:15
Arun AC25-May-04 2:15 
AnswerRe: how to create temporary canvas Pin
Anthony_Yio25-May-04 2:22
Anthony_Yio25-May-04 2:22 
GeneralRegex Pin
shiraztk25-May-04 1:24
shiraztk25-May-04 1:24 
GeneralRe: Regex Pin
Anthony_Yio25-May-04 2:18
Anthony_Yio25-May-04 2:18 
Generala question about email address of america Pin
includeh1025-May-04 0:53
includeh1025-May-04 0:53 
GeneralRe: a question about email address of america Pin
Maximilien25-May-04 0:59
Maximilien25-May-04 0:59 
General830305 - Invalid adjustment in RTL menus Pin
ilostmyid225-May-04 0:26
professionalilostmyid225-May-04 0:26 
Generalopen file dialog in wrong position Pin
si_6924-May-04 23:20
si_6924-May-04 23:20 
GeneralRe: open file dialog in wrong position Pin
jmkhael24-May-04 23:33
jmkhael24-May-04 23:33 
GeneralRe: open file dialog in wrong position Pin
si_6924-May-04 23:44
si_6924-May-04 23:44 
GeneralRe: open file dialog in wrong position Pin
jmkhael24-May-04 23:54
jmkhael24-May-04 23:54 
GeneralRe: open file dialog in wrong position Pin
si_6925-May-04 0:05
si_6925-May-04 0:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.