Solved

Initializing Iterators

Posted on 2013-05-15
3
519 Views
Last Modified: 2013-05-27
I have a piece of code converting from Microsoft Visual Studio 6 to 2008. When I compile the following under VS 2008:

	

// Declare the iterators that will hold the earliest requests and subrequests
// and initialize them to NULL
std::set<ShrPtr<Requests> >::const_iterator FirstRequest;
std::set<ShrPtr<SubRequests> >::const_iterator FirstSubRequest;
FirstRequest = NULL;
FirstSubRequest = NULL;

Open in new window


I get the following compiler error --


Requests.cpp(443) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
        c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xtree(419): could be 'std::_Tree<_Traits>::const_iterator &std::_Tree<_Traits>::const_iterator::operator =(const std::_Tree<_Traits>::const_iterator &)'
        with
        [
            _Traits=std::_Tset_traits<ShrPtr<Request>,std::less<ShrPtr<Request>>,std::allocator<ShrPtr<Request>>,false>
        ]
        while trying to match the argument list '(std::_Tree<_Traits>::const_iterator, int)'
        with
        [
            _Traits=std::_Tset_traits<ShrPtr<Request>,std::less<ShrPtr<Request>>,std::allocator<ShrPtr<Request>>,false>
        ]

I actually agree with the compiler and I am surprised this compiled at all under Visual Studio 6. The question then becomes how to do this.
0
Comment
Question by:volleyballjerry
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 39169328
VC6 is from a time where there was no standardization of C++, that followed later. So the implementation of the designed C++ features was not "standard" thus the above might have worked these days. In a nutshell, if you remove the lines

FirstRequest = NULL;
FirstSubRequest = NULL;

Open in new window


and replace them e.g. with

FirstRequest = Requests.begin();
FirstSubRequest = SubRequests.begin();

Open in new window


you should be fine. Alonve the removal of the 'NULL' assignments should do it.
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 250 total points
ID: 39171193
vc6 used simple pointers for most iterators of the stl. that's why you could assign a NULL.

with visual studio all these simple iterators have been implemented by class objects derived from iterator baseclass. those iterators don't support an initialization with NULL.

note, that doesn't mean that you can't use pointers where an iterator was required. for example, the following code will compile at older or newer systems:

int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
if (std::find(&arr[0], &arr[10], 7) != &arr[10])
    ...

Open in new window


here, pointers to a c array were used instead of iterators.

Sara
0
 

Author Closing Comment

by:volleyballjerry
ID: 39199212
Excellent information and this was a huge help - thank you!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
t-sql sp_addlinkedserver possible risks 3 94
Not seen Link button 5 58
No module found pypyodbc, 3 33
Graph API & MS Apps 1 24
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question