Thursday, July 9, 2009

Templates using C++ for a function minimum. The inside the brackets formula, mine is not any good.Thanks.?

You had a number of errors and I won't remember them all so do a diff (compare) on your code with this. Basically





- you were not making function calls


- you were not using ?: syntax correctly


- your template was not returning the value


- your template names conflicted with std::max and std::min


- main() wouldn't compile





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;





template %26lt;class T%26gt;


T Min(T num1, T num2)


{


return((num1 %26lt; num2) ? num1 : num2);


}





//Template definition for Max function.


template %26lt;class T%26gt;


T Max(T num1, T num2)


{


return((num1 %26gt; num2) ? num1 : num2);


}








int main()


{


int i1;


int i2;


double d1;


double d2;





cout %26lt;%26lt; "Enter 2 integers: ";


cin %26gt;%26gt; i1 %26gt;%26gt; i2;


cout %26lt;%26lt; "The lesser of the two numbers is: " %26lt;%26lt; Min(i1, i2) %26lt;%26lt; endl;








cout %26lt;%26lt; "Enter 2 doubles: ";


cin %26gt;%26gt; d1 %26gt;%26gt; d2;


cout %26lt;%26lt; "The lesser of the two numbers is: " %26lt;%26lt; Min(d1, d2) %26lt;%26lt; endl;





cout %26lt;%26lt; "Enter 2 integers: ";


cin %26gt;%26gt; i1 %26gt;%26gt; i2;


cout %26lt;%26lt; "The greater number is: " %26lt;%26lt; Max(i1, i2) %26lt;%26lt; endl;








cout %26lt;%26lt; "Enter 2 doubles: ";


cin %26gt;%26gt; d1 %26gt;%26gt; d2;


cout %26lt;%26lt; "The greater number is: " %26lt;%26lt; Max(d1, d2) %26lt;%26lt; endl;





return 0;


}

Templates using C++ for a function minimum. The inside the brackets formula, mine is not any good.Thanks.?
I'm afraid you'll have to give a lot more information before anyone can begin to help you. Maybe if you posted some code examples.


No comments:

Post a Comment