C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Templates and Different Compilers

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
demibee
Guest





PostPosted: Tue Dec 30, 2003 6:51 am    Post subject: Templates and Different Compilers Reply with quote



I just downloaded DJGPP 2.03 and I wanted to test it on something
simple. Unfortunately, I chose something I know little about -- C++
templates. (The code is practically copied directly from MS's own
webpages.) The following compiles in VS6 with 0 errors and warnings,
but the DJGPP compile gives errors indicating that the call to "min"
in the main program is ambiguous (errors are shown below the code).

Have I implemented the template incorrectly? Have I used gxx
incorrectly? Is VS lax when it comes to picking out problems? (I
notice it doesn't warn me that argc and argv are never used.)

Anyway, the code and DJGPP's error messages...

----------

/* TEST.CPP -
*/



#include<iostream>
#include<string>



using namespace std;



template <class T>
T min (T a, T b)
{
return (a < b) ? a : b;
}



int main (int argc, char *argv[])
{

cout << "min(5, 10) is: " << min(5, 10) << endl;
cout << "min(2.5, 1.0) is: " << min(2.5, 1.0) << endl;
cout << "min('A', 'a') is: " << min('A', 'a') << endl;

return 0;

} // end main

----------
C:SRCCPP> gxx test.cpp -o test.exe
test.cpp: In function `int main(int, char**)':
test.cpp:27: error: call of overloaded `min(int, int)' is ambiguous
test.cpp:18: error: candidates are: T min(T, T) [with T = int]
C:/APPS/DOS/DJGPP/lang/cxx/3.32/bits/stlalgobase.h:149: error:
const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = int]
test.cpp:28: error: call of overloaded `min(double, double)' is
ambiguous
test.cpp:18: error: candidates are: T min(T, T) [with T = double]
C:/APPS/DOS/DJGPP/lang/cxx/3.32/bits/stlalgobase.h:149: error:
const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = double]
test.cpp:29: error: call of overloaded `min(char, char)' is ambiguous
test.cpp:18: error: candidates are: T min(T, T) [with T = char]
C:/APPS/DOS/DJGPP/lang/cxx/3.32/bits/stlalgobase.h:149: error:
const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = char]
----------


db

Back to top
CrayzeeWulf
Guest





PostPosted: Tue Dec 30, 2003 7:36 am    Post subject: Re: Templates and Different Compilers Reply with quote



demibee wrote:
Quote:

Have I implemented the template incorrectly? Have I used gxx
incorrectly? Is VS lax when it comes to picking out problems? (I
notice it doesn't warn me that argc and argv are never used.)

A template with the same name and number of parameters is part of the "std"

namespace in the header <algorithm>. DJGPP probably includes this through
<string> and results in the conflict. You can just use the standard min()
template by explicitly including <algorithm> as follows:

// -------------------------------------------------------------------
#include <iostream>
#include <string>
#include <algorithm>


int main (int argc, char *argv[])
{
using namespace std ;
cout << "min(5, 10) is: " << min(5, 10) << endl ;
cout << "min(2.5, 1.0) is: " << min(2.5, 1.0) << endl ;
cout << "min('A', 'a') is: " << min('A', 'a') << endl ;
return 0;
} //end main
// -------------------------------------------------------------------

Otherwise, if you insist on using your own template, consider the following
workaround by not injecting everything from the "std" namespace into the
global namespace (i.e. remove "using namespace std"):

#include #include <string>

template <class T>
T min (T a, T b)
{
return (a < b) ? a : b;
}

int main (int argc, char *argv[])
{
std::cout << "min(5, 10) is: " << min(5, 10) << std::endl ;
std::cout << "min(2.5, 1.0) is: " << min(2.5, 1.0) << std::endl ;
std::cout << "min('A', 'a') is: " << min('A', 'a') << std::endl ;
return 0;
} //end main


Hope that helps,
--
CrayzeeWulf

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.