www.digitalmars.com         C & C++   DMDScript  

c++ - Template error only when windows.h is included

reply Kramer <Kramer_member pathlink.com> writes:
I was trying to create a function template using a Windows function.  However I
kept getting an error I didn't know how to resolve.  This example is contrived,
but it illustrates the error.  I know DigitalMars didn't write windows.h, but I
just started using DMC, so I was hoping someone has run across this before and
knows of a solution.

Here's the code:
##include <windows.h>
##include <iostream>

#using namespace std;

#template <typename T>
#T max(T a, T b)
#{
#    return a > b ? a : b ;
#}

#int main(int argc, char *argv[])
#{
#    cout << max(10, 15);
#    return 0;
#}

When I compile using the compile command: dmc Test20050307_1.cpp
-Ic:\dm\stlport\stlport

I get this error: Test20050307_1.cpp(7) : Error: closing ')' of '( declarator )'
expected
--- errorlevel 1

However, if you comment out windows.h, using the compile command above, it
compiles clean.

I'm just really confused, because the code looks correct to me.

-Kramer
Mar 07 2005
parent Christof Meerwald <cmeerw web.de> writes:
On Mon, 7 Mar 2005 17:18:21 +0000 (UTC), Kramer wrote:
 I was trying to create a function template using a Windows function.  However I
 kept getting an error I didn't know how to resolve.  This example is contrived,
 but it illustrates the error.  I know DigitalMars didn't write windows.h, but I
 just started using DMC, so I was hoping someone has run across this before and
 knows of a solution.

 Here's the code:
 ##include <windows.h>
 ##include <iostream>

 #using namespace std;

 #template <typename T>
 #T max(T a, T b)
 #{
 #    return a > b ? a : b ;
 #}

I believe windows.h defines "max" as a macro (unless you compile with -DNOMINMAX) bye, Christof -- http://cmeerw.org mailto:cmeerw at web.de xmpp:cmeerw at cmeerw.org ...and what have you contributed to the Net?
Mar 08 2005