CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2002
    Posts
    36

    Codewarrior 5 Query

    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.

    Code:
    #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.

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Maybe showing the exact error messages would help.

  3. #3
    Join Date
    Oct 2002
    Posts
    36
    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;

    Code:
    template <class T>
    inline
    const T&
    min(const T& a, const T& b)
    {
    	return b < a ? b : a;
    }
    Which looks pretty standard to me.

    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....

  4. #4
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    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?
    Succinct is verbose for terse

  5. #5
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Windows.h defines min() and max() as macros. #undef these before including <string> and see what happens.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  6. #6
    Join Date
    Oct 2002
    Posts
    36
    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++).

    Thank you all for responding and thank you Graham for the answer.

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    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.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340
    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.

  9. #9
    Join Date
    Oct 2002
    Posts
    36
    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
    Code:
    #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>
    Last edited by dumah; October 26th, 2002 at 06:38 AM.

  10. #10
    Join Date
    Jul 2002
    Location
    American Continent
    Posts
    340
    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;

  11. #11
    Join Date
    Oct 2002
    Posts
    36
    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/...r/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
    Last edited by dumah; October 26th, 2002 at 02:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured