473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Templates and Different Compilers

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:\SRC\CPP> 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

Jul 22 '05 #1
1 1350
demibee wrote:

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 <iostream>
#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
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Bernd Fuhrmann | last post by:
Hi! I just tried to write a little program, but GCC (Mingw 3.3.1) refused to compile it. So I'd like to know if there's something wrong with my code or if it is a bug in GCC. ---snip---...
6
by: Greg Warren | last post by:
I am writing a container class so that I can reuse my double ended list code. I have a base class CListBase that handles inserts and deletes with a member function addItem, defined as:...
11
by: Elpoca | last post by:
Hi: What rules govern the inlining of templated functions and templated class methods? It has always been my understanding that both templated functions and templated class methods were...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
14
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to...
5
by: ryanoasis | last post by:
Working on a C++ assignment and I cant figure out the problems I am having w/ Templates and Subclasses. I know there are issues with templates and certain compilers so I am not sure what the...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
6
by: pleexed | last post by:
hello, this is my first post in a newsgroup, i hope i do everything right :) first of all, i am sure there have been a lot of "are templates slow?" questions around, but i think what i would...
26
by: puzzlecracker | last post by:
Team, C++ has been around since 1986, why templates are still regarded is a new feature by most compiler vendors and not fully supported (for example export feature). Look at other popular...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.