![]() |
|
#1
|
|||
|
|||
About min and maxI have a matrix library with a namespace which have some min max operations. I understand that windows.h have macros for min max. I really don't why the compiler issues me error when i call the library min max. By the way i did a simple console program to illustrate. (Using VS2005)
CPP / C++ / C Code:
I thought with namespace prefix M would resolve this conflict, but i was wrong. I wonder if there's any other solution? |
|||
|
#2
|
|||
|
|||
Re: About min and maxQuote:
I note that some compiler vendors have implemented min() and max() in such a way that C++ programs with <windows.h> and min() and max() have implemented these as templated functions, so your program might very well compile without the #undef thingie. Of course that wouldn't help at all for C programs that use <windows.h>. See Footnote. Quote:
Sure. Don't use <windows.h> or any other non-standard C++ headers. If you are creating a new program and writing your own classes or other functions, don't use #defined macros in for functions in your header files. Use templated functions or inline functions or whatever... If that's not an option, then you can change your function's name to something that doesn't conflict with anything in any of the header files that you do include. For example, call your function m_max() or some such thing. By the way, for users of <windows.h>, you might discover that older versions of Microsoft C++ libraries also implement things like abs() with #defined macros, but later versions use templated functions. See Footnote (again). Regards, Dave Footnote: This is one reason that, speaking for myself, I generally advise programmers not to use #defined macros for functions. Sometimes there may seem to be a reasonably good reason for new programs to do this, but new programmers don't always realize what problems can arise from gratuitous use of #defined macros. Bad habits formed early in one's development are hard to break. And, in case people feel obliged to point out that actually there are some things that are reasonably done with #defined macros used as functions: I know that. If people get to the point in their development as programmers where they want do use them, I won't argue. (But I might reiterate my misgivings, and might even give a counterexample.) My advice is just an opinion. Free advice, freely given, and worth exactly what you want it to be worth. Period. Full stop. Last edited by davekw7x : 19-Mar-2010 at 08:39.
|
|
#3
|
|||
|
|||
Re: About min and maxActually i never include windows.h. Thats why, i'm even more puzzled.
I haven try with the STL's std::min std::max......... i wonder if it too conflicts with the windows.h min max. Anyway many thanks, Dave (footnote guy)!! |
|
#4
|
|||
|
|||
Re: About min and maxQuote:
Anyhow: When I comment out the #ifdef stuff, your little do-nothing programlet compiles OK with my versions of Microsoft Visual C++ compilers (Version 6.0, 2005 Express, 2008 Express), using the command line Code:
How are you compiling? If it's from the Visual Studio IDE, maybe some project settings include some stuff that creates the conflict. I mean, I don't see any explanation for the behavior that you described unless the compiler is, somehow, somewhere, encountering a #defined macro for max(). So, my other recommendation is still an option: If you don't like the ugliness of the #ifdef stuff, you can change the name of your function. Regards, Dave Footnote: "They say that nobody is perfect. Then they tell you practice makes perfect. I wish they'd make up their minds." --Wilt Chamberlain Nobody roots for Goliath Last edited by davekw7x : 19-Mar-2010 at 10:06.
|
|
#5
|
|||
|
|||
Re: About min and maxWith the #defines commented out I get this:
Code:
gcc version 4.1.1 20061011 (Linux Red Hat 4.1.1-30) |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The