VC6 -> VC2003 STL compiler error

Hello, I'm having problems porting STL to VC2003.  If anyone could please 
help me convert this code that compiles fine with VC6 to compile with 
VC2003, I'd be grateful:

class StrBuffer: private std::vector<_TCHAR>
{
    typedef std::vector<_TCHAR> Base;

public:

    LPTSTR operator&()
    {
        return Base::begin();    // <-- compiler error

        // The above line's error is:
        //  error C2440: 'return' : cannot convert from 
'std::vector<_Ty>::iterator *' to 'LPTSTR'
        //   with
        //  [
        //     _Ty=_TCHAR
        // ]
        // Types pointed to are unrelated; conversion requires 
reinterpret_cast, C-style cast or function-style cast
    }
}



Another problem code:

    DWORD d1 = 5;
    DWORD d2 = 10;
    DWORD max = std::_cpp_max(d1, d2);

    // the above line gives:    error C2589: '(' : illegal token on right 
side of '::'
    // error C2059: syntax error : '::'


Thanks much,
David



0
dc2983 (3206)
11/25/2006 3:13:05 PM
vc.mfc 33608 articles. 0 followers. Follow

4 Replies
1125 Views

Similar Articles

[PageSpeed] 57

David Ching wrote:

> Hello, I'm having problems porting STL to VC2003.  If anyone could please 
> help me convert this code that compiles fine with VC6 to compile with 
> VC2003, I'd be grateful:
> 
> class StrBuffer: private std::vector<_TCHAR>
> {
>     typedef std::vector<_TCHAR> Base;
> 
> public:
> 
>     LPTSTR operator&()
>     {
>         return Base::begin();    // <-- compiler error
> 
>         // The above line's error is:
>         //  error C2440: 'return' : cannot convert from 
> 'std::vector<_Ty>::iterator *' to 'LPTSTR'
>         //   with
>         //  [
>         //     _Ty=_TCHAR
>         // ]
>         // Types pointed to are unrelated; conversion requires 
> reinterpret_cast, C-style cast or function-style cast
>     }
> }
> 
> 
> 
> Another problem code:
> 
>     DWORD d1 = 5;
>     DWORD d2 = 10;
>     DWORD max = std::_cpp_max(d1, d2);
> 
>     // the above line gives:    error C2589: '(' : illegal token on right 
> side of '::'
>     // error C2059: syntax error : '::'
> 
> 
> Thanks much,
> David
David:

1. Iterators are not necessarily pointers. They just happen to be in VC6.

Try

return &(*Base::begin());

2. The use of _cpp_max was an ugly workaround for the fact that windows 
#defines the symbols min and max.

I think in VC2003 you can/must use std::min and std::max. I always did 
this in VC6 also, by using

#define NOMINMAX

in the stdafx.h, and then defining std::min and std::max for myself 
(defying the standard by adding to the std namespace, but then these 
things should never have been missing in the first place ..).

You probabaly need to define NOMINMAX in VC2003 also. This can cause 
other problems, but in my experience they can be worked around.

David Wilkinson
0
no-reply8010 (1791)
11/25/2006 4:05:32 PM
"David Wilkinson" <no-reply@effisols.com> wrote in message 
news:eN80JuKEHHA.3668@TK2MSFTNGP06.phx.gbl...
> 1. Iterators are not necessarily pointers. They just happen to be in VC6.
>
> Try
>
> return &(*Base::begin());
>
> 2. The use of _cpp_max was an ugly workaround for the fact that windows 
> #defines the symbols min and max.
>
> I think in VC2003 you can/must use std::min and std::max. I always did 
> this in VC6 also, by using
>
> #define NOMINMAX
>
> in the stdafx.h, and then defining std::min and std::max for myself 
> (defying the standard by adding to the std namespace, but then these 
> things should never have been missing in the first place ..).
>
> You probabaly need to define NOMINMAX in VC2003 also. This can cause other 
> problems, but in my experience they can be worked around.
>

Thank you so much, David.  Both of your fixes worked.  I never would have 
figured this out!  I had to #define NOMINMAX in the stdafx.h, before 
including any header files.  :-)

-- David




0
dc2983 (3206)
11/25/2006 4:31:59 PM
"David Wilkinson" <no-reply@effisols.com> wrote in message 
news:eN80JuKEHHA.3668@TK2MSFTNGP06.phx.gbl...
> 1. Iterators are not necessarily pointers. They just happen to be in VC6.
>
> Try
>
> return &(*Base::begin());
>

This compiled in VC2005 but at runtime I got this error box:  "vector 
iterator not dereferencable".  On the advice of others, I changed

    &(*Base::begin()) --> &Base::front()


but I still got the error because front() is undefined when the vector is 
empty.  I fixed this problem by changing calls to reserve() to resize() so 
that the elements are physically allocated.

BTW, this is why I say STL is not intuitive.  When you reserve() something, 
you expect the space is committed so you should be able to access the 
elements.  But no, a call to resize(), which by the name is totally 
unintuitive how it is different than reserve(), is needed.  Anyway, I 
appreciate your expert help, David.

Thanks,
David 


0
dc2983 (3206)
11/27/2006 5:18:45 AM
On Mon, 27 Nov 2006 05:18:45 GMT, "David Ching"
<dc@remove-this.dcsoft.com> wrote:

>    &(*Base::begin()) --> &Base::front()
>
>
>but I still got the error because front() is undefined when the vector is 
>empty.  I fixed this problem by changing calls to reserve() to resize() so 
>that the elements are physically allocated.

David: what about trying something like:

    empty() ? NULL : (&Base::front())

reserve() is associated with the "capacity" of the vector, and I think
that maybe your original design of using capacity and not "size" (so
reserve() instead of resize()) was the better one (better than
changing calls to reserve() to resize()).

>BTW, this is why I say STL is not intuitive.  When you reserve() something, 
>you expect the space is committed so you should be able to access the 
>elements.  But no, a call to resize(), which by the name is totally 
>unintuitive how it is different than reserve(), is needed.

I think about reserve() as an "hint" to the vector class instance,
exspecially if you know that you will add lots of elements to the
vector. With reserve() you pre-allocate room in the vector, and avoid
frequent memory reallocation. If you then insert items using
push_back(), you don't have to think about resizing() the vector.

MrAsm
0
invalid460 (432)
11/28/2006 10:09:02 PM
Reply:

Similar Artilces: