Click to See Complete Forum and Search --> : Codewarrior 5 Query
dumah
October 25th, 2002, 09:42 AM
I have a copy of Codewarrior which I use for C++ (as well as VC++ and Devc++).
When I work in console mode, I have no problem with STL, but if I try use say a std::string in a windows GUI app, I get a multitue of errors.
#include <windows.h>
#include <string>
int WINAPI WinMain( HINSTANCE hInst,
HINSTANCE hPreInst,
LPSTR lpszCmdLine,
int nCmdShow )
{
std::string str("Hello World");
MessageBox(NULL,str.c_str(),NULL,MB_OK);
return 0;
}
This works great on VC++, but chokes a painful death on Codewarrior 5. I get errors relating to algobase.h and limits.h
I have searched the web for a resource on bugs ect (I got the package free with a magazine in the UK, so I dont have Metroworks support).
Anyone with any ideas - I would welcome your comments.
Yves M
October 25th, 2002, 10:33 AM
Maybe showing the exact error messages would help.
dumah
October 25th, 2002, 10:38 AM
Originally posted by Yves M
Maybe showing the exact error messages would help.
Well the errors are syntax errors relating to the headers provided....
For example
"Error - ')' Expected - algobase.h line 29"
This realtes to the code sequence;
template <class T>
inline
const T&
min(const T& a, const T& b)
{
return b < a ? b : a;
}
Which looks pretty standard to me. :confused:
As I said, this error on the GUI projects....if its a Win32 Console, there's no problem. I dont know if its a problem with my compiler (bugs) or perhaps a setting I am missing....
cup
October 25th, 2002, 01:12 PM
It seems to work fine on my copy of Code Warrior. I have version 4.0.4. Maybe it is one of the compile time switches. Did you create it as a win32 C++ project?
Graham
October 25th, 2002, 01:13 PM
Windows.h defines min() and max() as macros. #undef these before including <string> and see what happens.
dumah
October 25th, 2002, 01:48 PM
Originally posted by Graham
Windows.h defines min() and max() as macros. #undef these before including <string> and see what happens.
Excellent!!!
Doh!!!! - I should have seen that!!!! That works a treat now ;)
Weird how this conflict didnt show up under My other compilers (VC++6 & DevC++). :confused:
Thank you all for responding and thank you Graham for the answer.
Graham
October 25th, 2002, 04:11 PM
I've been hit by it in the past, which why I recognised it as soon as I saw the "min". (BTW, you'll also get the same problem if you try to use std::numeric_limits with windows.h)
It's just one example of why it's best to avoid #define.
AnthonyMai
October 25th, 2002, 05:59 PM
I've been hit by it in the past, which why I recognised it as soon as I saw the "min". (BTW, you'll also get the same problem if you try to use std::numeric_limits with windows.h)
It's just one example of why it's best to avoid #define.
Graham: You can discuss the good or bad of #define some where else. But in this specific case you made a bad case against #define.
The fault here is not #define. You can replace #define with any thing else you want. The problem is the keyword min has ALREADY BEEN used by one of Microsoft's header file. I don't care how that name is being used, you should NEVER define something else that uses the same name, EVER.
It's CodeWarrior's fault that they used the name "min" to define one of their template functions in the CodeWarrior specific header file algobase.h.
Once you start to use the same name for different things, you've opened a whole can of worms. I don't think #undef Microsoft's definition of min, like Graham did, is a good solution. What if there is some code some where in either the header files or the cpp files, that EXPECTED the min to be defined as what Microsoft defined it?
Renaming the CodeWarrior's min to something with different spelling should be a better solution.
dumah
October 26th, 2002, 06:26 AM
To my knowledge, the max and min template functions are part of the stl (though why the namespace resolution didnt resolve this error is a mystery to me - maybe the preprocessor treats macros differently in this compiler to my others??) and so CodeWarrior cant really decide not to implement them as is, and it wouldnt be a good idea to change them IMO
Looking at the Windows max func
#define max(a, b) (((a) > (b)) ? (a) : (b))
I think that that's the one that should be discarded IMO :). Scott Meyers devoted half of point 1 in his Effective C++ book to this exact macro and its many downfalls.
Personally, I try to avoid the preprocessor as much as possible.
Thanks again to all who responded
<edit>
Actually, looking at MSVC++ help files for the STL version of max;
Microsoft-Specific:
To avoid conflicts with min and max in WINDEF.H, use _MIN and _MAX instead. These macros evaluate to _cpp_min and _cpp_max, respectively.
END Microsoft-Specific
That seems to be how thay get over this</edit>
AnthonyMai
October 26th, 2002, 10:30 AM
To my knowledge, the max and min template functions are part of the stl (though why the namespace resolution didnt resolve this error is a mystery to me - maybe the preprocessor treats macros differently in this compiler to my others??) and so CodeWarrior cant really decide not to implement them as is, and it wouldnt be a good idea to change them IMO
To my knowledge, unless your knowledge is better than mine, there is NOT a max or min templated function specified in STL. There is only a max and min member function defined for the template class numeric_limits.
name space would not help you to resolve name conflict with a macro defined using the same name. That's because macros names are not really names. It simply get replaced during the pre-process. name space only helps to resolve name conflict after the code is compiled and before it is linked. At that time all names are mangled with their name space, name of the class they belong to, and other things so there is no longer a name conflict. But by that time, the macro names has already disappeated and be replaced.
You should NEVER EVER use the same name to refer to any thing, EVER. If you want to use a name that you suspect could be commonly used, do a global search to make sure that name has not already been used some where else.
The only acceptable exception, would be calling a structure's tag name and type name the same thing. In this case there is little chance of a confusion. Like:
[code]
typedef struct MYSTRUCTNAME
{
// structure definition
} MYSTRUCTNAME;
dumah
October 26th, 2002, 01:20 PM
Originally posted by AnthonyMai
To my knowledge, unless your knowledge is better than mine, there is NOT a max or min templated function specified in STL. There is only a max and min member function defined for the template class numeric_limits.
Sorry, maybe I should have said the max & min template functions from the C++ Standard Library instead of STL (C++ Programming Language - Stroustrup 18.9)
http://wwwinfo.cern.ch/asd/lhc++/RW/stdlibcr/max_6671.htm
or
http://www.josuttis.com/libbook/util/minmax1.cpp.html
or
http://www.sgi.com/tech/stl/max.html
Originally posted by AnthonyMai
name space would not help you to resolve name conflict with a macro defined using the same name. That's because macros names are not really names. It simply get replaced during the pre-process. name space only helps to resolve name conflict after the code is compiled and before it is linked.
Thinking about it, that makes sense.....
macros - tricky devils ;)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.