what wrong with this function template?
-
#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
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 ) ;
-
#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
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.
-
#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
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
-
#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
There isn't per se anything wrong with the template function, rather with some other parts of the code.
- You are using
iostream.h
, this is a pre-standard header and should not be used. Instead useiostream
, without a postfixed .h - You defined two functions manually,
float max(float, float);
andint 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. - 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[^] - 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).
- Lastly, the main signature is according to the standard either
int main()
orint 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/[^
- You are using
-
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
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.
-
There isn't per se anything wrong with the template function, rather with some other parts of the code.
- You are using
iostream.h
, this is a pre-standard header and should not be used. Instead useiostream
, without a postfixed .h - You defined two functions manually,
float max(float, float);
andint 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. - 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[^] - 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).
- Lastly, the main signature is according to the standard either
int main()
orint 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/[^
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 - You are using
-
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. DJRead 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 typemax(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/[^]) -
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.
-
but if I just comment out int
maxf(int, int);
, the code works. Moreover, if I comment out floatmaxf(float, float);
, the code works too. DJso comment out both if u like i'm online (invis always) now with id ilostmyid2 at yahoo messenger
-
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 typemax(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/[^])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 leftint 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
-
so comment out both if u like i'm online (invis always) now with id ilostmyid2 at yahoo messenger
thanks. my concern is why
int max(int, int)
not working butfloat 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
-
thanks. my concern is why
int max(int, int)
not working butfloat 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 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
-
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 leftint 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
As ilostmyid2 pointed out then
TFun(5.4321, 1.2345)
calls the template function withT = double
, if you want to use thefloat TFun(float, float);
function instead you should call it withTFun(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/[^]) -
#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