Venu557822

Hi, I am trying to compile VC++6 code in 2005. I am having problems with NULL any one can explain me how do I fix this error message

My code is as below..

static vector <int>::iterator data = NULL;

I got the error message below.

f:\test\test.cpp(20) : error C2440: 'initializing' : cannot convert from 'int' to 'std::_Vector_iterator<_Ty,_Alloc>'

1> with

1> [

1> _Ty=int,

1> _Alloc=std::allocator<int>

1> ]

1> No constructor could take the source type, or constructor overload resolution was ambiguous



Re: Visual C++ Language Problem with NULL in visualstudio 2005

Shakje

You really need to provide some context for this code, but the problem is that you need to set the iterator to another iterator value. NULL just means 0, and it works when you set a pointer to it because the pointer simply gets told to point to 0 (which of course isn't an accessible memory address). When you try to assign something to data it tries to assign a vector<int>::iterator, not a number.

What you should do to initialise an iterator as NULL is use the default constructor, so..

static vector<int>::iterator data();






Re: Visual C++ Language Problem with NULL in visualstudio 2005

Viorel.

The STL iterators were improved so I think you have to improve your program too. For a similar discussion on same topic see http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=403438&SiteID=1.

Assigning of NULL to iterators was probably an undocumented feature of older STL, which now is abrogated.

In my opinion, if your nullable iterators appear in comparison operations like this:

Code Snippet

static vector< int >::iterator data = NULL;

. . .

if( data != NULL)

{

. . .

}

then I would propose the following adjustment:

Code Snippet

static vector< int > unused;

static vector< int >::iterator data = unused.end();

. . .

if( data != unused.end())

{

. . .

}

I.e. instead of working with NULL, use an allowed distinct value.

I hope this makes sense.





Re: Visual C++ Language Problem with NULL in visualstudio 2005

Marius Bancila

Well, you should simply compare iterators againt container's end() not NULL.




Re: Visual C++ Language Problem with NULL in visualstudio 2005

Venu

Hi All..

Thank you so much for all your help.

The problem was resolved by defining _HAS_ITERATOR_DEBUGGING and _SECURE_SCL in stdafx.h

#ifndef _HAS_ITERATOR_DEBUGGING

#define _HAS_ITERATOR_DEBUGGING 0

#endif

#ifndef _SECURE_SCL

#define _SECURE_SCL 0

#endif

Thank you,

Venu.





Re: Visual C++ Language Problem with NULL in visualstudio 2005

Venu

Hi All..

Thank you so much for all your help.

The problem was resolved by defining _HAS_ITERATOR_DEBUGGING and _SECURE_SCL in stdafx.h as below.

#ifndef _HAS_ITERATOR_DEBUGGING

#define _HAS_ITERATOR_DEBUGGING 0

#endif

#ifndef _SECURE_SCL

#define _SECURE_SCL 0

#endif

Thank you,

Venu.





Re: Visual C++ Language Problem with NULL in visualstudio 2005

Marius Bancila

I consider that the wrong approach. Instead of fixing the problem (because it is a problem) you simply ignore it.