|
#include <iostream.h>
template<class t=""> 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 ) ;
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
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
|
|
|
|
|
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 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 <iostream>
template<class T> T TFun(T a, T b)
{
if (a > b)
return(a);
else
return(b);
}
float TFun(float, float);
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
|
|
|
|
|
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 use iostream , without a postfixed .h - 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. - 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() 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/[^])
|
|
|
|
|
thanks a lots.
now this code works:
#include <iostream><br />
<br />
template<class T> T max(T a, T b)<br />
{<br />
if (a > b)<br />
return(a);<br />
else<br />
return(b);<br />
}<br />
<br />
<br />
void main(void)<br />
{<br />
std::cout << "The maximum of 100 and 200 is " << <br />
max(100, 200) << '\n';<br />
<br />
std::cout << "The maximum of 5.4321 and 1.2345 is " << <br />
max(5.4321, 1.2345) << '\n';<br />
}
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
|
|
|
|
|
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/[^])
|
|
|
|
|
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 <iostream>
template<class T> T TFun(T a, T b)
{
if (a > b)
return(a);
else
return(b);
}
float TFun(float, float);
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 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/[^])
|
|
|
|
|
thanks for your quick reply.
DJ
|
|
|
|
|
hai all,
I am doing a project in MFC involving graphics.
I used pointerlist to add all the elements(line ,rectangle
or ellipse) drawn on the screen.
And,I redraw(refresh) all the elements in the pointer list,
while drawing a new element.
When more elements are added,the drawing gets very slow.
Is there any way to avoid this slowness without affecting the
previously drawn elements?
Plz help me.
With thanks,
arun chakaravarthy
|
|
|
|
|
check double buffering.
Sonork 100.41263:Anthony_Yio
Life is about experiencing ...
|
|
|
|
|
Hi
I want to use Regex class in my VC++ 7 application. I am familiar with VC++ 6. I want to use the .net framework in VC++ 7 application.
How do I include the header to use the Regex class ?
please anyone help me...
|
|
|
|
|
Just
using namespace System.Text.RegularExpressions;
and add reference System.Text.dll through project or
through code
#using <System.Text.dll>
and I suppose you have start up your project as .NET app in C++.
If you are not you will need to include
#using <mscorlib.dll>
#using <System.Text.dll>
and set your project setting to support clr.
and etc....
<B>Sonork 100.41263:Anthony_Yio
Life is about experiencing ... </B>
|
|
|
|
|
Hi all,
it is not a c++ question.
i am not in america and sent several emails to america universities before yesterday, but i did't get any feedback.
the email address looks like: xxx@yyy.edu
i suspect that i need to add .us if out of america as
xxx@yyy.edu.us
am i right? do u know which way is correct?
thx
includeh10
|
|
|
|
|
includeh10 wrote:
i am not in america and sent several emails to america universities before yesterday, but i did't get any feedback.
No news is good news, it means the mail did'nt bounced back, so the address should be ok ( have you checked on the web sites or other documentations ? ) Also, you might not be the only one trying to contact them by email, and it can take a while; I don't think most of them use "read-receipt"
I think most US universities use .edu, not .edu.us
Maximilien Lincourt
Your Head A Splode - Strong Bad
|
|
|
|
|
hi all
i set up a right to left menu in my language for my application. but the items are not right aligned. it seems to be a bug in calculation of their widths:
http://home.morva.net/hamidi/menu-ye_kharab.gif
because when i use a fixed width font for menus in Appearance section of Desktop Properties, the problem gets fixed.
anyone has an idea or knows a workaround?
thanx
|
|
|
|
|
Hi
i am calling the open file dialog in my program
if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
however,when it displays it is placed in the top left of the screen
and not in the middle
can any1 help
thanks
si
|
|
|
|
|
Did you specify the HWND hwndOwner? in the ofn?
Papa
while (TRUE)
Papa.WillLove ( Bebe ) ;
|
|
|
|
|
Papa wrote:
Did you specify the HWND hwndOwner? in the ofn?
i have declared
HWND hwnd;
ofn.hwndOwner = hwnd;
but get a runtime error, when it executes
|
|
|
|
|