STL code compiling problems in VS 2005

A

ankan.banerjee

I have a piece of STL code which compiled perfectly with VC++ 6.0 and
VS .NET 2003 but I get loads of errors for it in VS 2005.

The code is as follows:

class CTimeKey: public pair<wstring, wstring> {};
class CTimeFactors
{
public:
double calendar_factor;
double calendar_days;
bool pattern_check;
double pattern_factor;
};

class CTimeLine: public map<CTimeKey, CTimeFactors> {};

CTimeLine::iterator l_time_iterator;
....
if (l_time_iterator == NULL) {
....
}

The error is for the NULL comparison operation and is as follows:

error C2678: binary '==' : no operator found which takes a left-hand
operand of type 'std::_Tree<_Traits>::iterator' (or there is no
acceptable conversion)
with
[

_Traits=std::_Tmap_traits<CTimeKey,CTimeFactors,std::less<CTimeKey>,std::allocator<std::pair<const
CTimeKey,CTimeFactors>>,false>
]
C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK
\include\guiddef.h(192): could be 'int operator ==(const GUID &,const
GUID &)'
C:\Program Files\Microsoft Visual Studio 8\VC\include
\xtree(287): or 'bool std::_Tree<_Traits>::const_iterator::blush:perator
==(const std::_Tree<_Traits>::const_iterator &) const'
with
[

_Traits=std::_Tmap_traits<CTimeKey,CTimeFactors,std::less<CTimeKey>,std::allocator<std::pair<const
CTimeKey,CTimeFactors>>,false>
]
while trying to match the argument list
'(std::_Tree<_Traits>::iterator, int)'
with
[

_Traits=std::_Tmap_traits<CTimeKey,CTimeFactors,std::less<CTimeKey>,std::allocator<std::pair<const
CTimeKey,CTimeFactors>>,false>
]


Any ideas how to resolve this?

Thanks and regards,
Ankan
 
C

Carl Daniel [VC++ MVP]

I have a piece of STL code which compiled perfectly with VC++ 6.0 and
VS .NET 2003 but I get loads of errors for it in VS 2005.

Well, first you should probably post in a C++ newsgroup instead of a C#
newsgroup.
The code is as follows:

class CTimeKey: public pair<wstring, wstring> {};
class CTimeFactors
{
public:
double calendar_factor;
double calendar_days;
bool pattern_check;
double pattern_factor;
};

class CTimeLine: public map<CTimeKey, CTimeFactors> {};

CTimeLine::iterator l_time_iterator;
...
if (l_time_iterator == NULL) {

[snipped more code]

Any ideas how to resolve this?

Iterators are not required to be comparable to null. It might work on some
implementations, but there's no guarantee. Instead, you need to compare the
iterator to another iterator, such as the one returned by end():

if (l_time_iterator == end())

It's also not a good idea to derive from std::pair or std::map since those
classes weren't designed to work as a base class. As long as you don't ever
call delete through a pointer to the base class, you won't have any
problems, but it's usually recommended to avoid the construct in the first
place.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top