Sign in to follow this  
utilae

Vector Errors during compile in Visual Studio 2008

Recommended Posts

utilae    188
Hi, I am using C++ and am compiling my code in Visual Studio 2008 on Windows XP. Previously my code compiled fine in Visual Studio 2003, but now in VS2008 it gives me the following two errors. Maybe I don't have visual studio 2008 setup right. Any ideas. Thanks.
1>error C2440: 'initializing' : cannot convert from 'int' to 'std::_Vector_iterator<_Ty,_Alloc>'
1>        with
1>        [
1>            _Ty=RECT,
1>            _Alloc=std::allocator<RECT>
1>        ]
1>        No constructor could take the source type, or constructor overload resolution was ambiguous


1>error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion)
1>        with
1>        [
1>            _Ty=RECT,
1>            _Alloc=std::allocator<RECT>
1>        ]
1>        c:\program files\microsoft sdks\windows\v6.0a\include\guiddef.h(197): could be 'int operator !=(const GUID &,const GUID &)'
1>        c:\program files\microsoft visual studio 2008\vc\include\vector(214): or       'bool std::_Vector_const_iterator<_Ty,_Alloc>::operator !=(const std::_Vector_const_iterator<_Ty,_Alloc> &) const'
1>        with
1>        [
1>            _Ty=RECT,
1>            _Alloc=std::allocator<RECT>
1>        ]
1>        while trying to match the argument list '(std::_Vector_iterator<_Ty,_Alloc>, int)'
1>        with
1>        [
1>            _Ty=RECT,
1>            _Alloc=std::allocator<RECT>
1>        ]

Share this post


Link to post
Share on other sites
utilae    188
Hi, ok.

The first error refers to this piece of code:


vector<RECT>::iterator itSearchRect=lstFilledRects.begin();
vector<RECT>::iterator itBestRect=NULL; //<<<<<===error 1 pointing here
int nSrcRectMaxBottom=0, nSrcRectMinLeft=recContainer.right;
while(itSearchRect!=lstFilledRects.end())
{



The second error refers to this piece of code:


if(itBestRect!=NULL) //<<<<<===error 2 pointing here
{
RECT recSrcHole;
recSrcHole.top=itBestRect->top;
recSrcHole.left=itBestRect->left;
recSrcHole.right=itBestRect->right;
recSrcHole.bottom=itBestRect->bottom;

Share this post


Link to post
Share on other sites
utilae    188

if(itBestRect!=NULL)




Why does functions I used to use not work?

There was a function for vectors such as Empty()

But it doesn't come up?


I think I somehow still need to make my settings in Visual Studio 2008 as unmanaged / native.

Share this post


Link to post
Share on other sites
Red Ant    471
std::vector never had a function called Empty(). It did and does, however, have an empty() method.

When things that used to work with an old compiler suddenly stop working with a newer compiler, it's often because the newer compiler is stricter and more standard-compliant and thus no longer allows you to get away with certain dirty tricks.

Share this post


Link to post
Share on other sites
utilae    188
Spelling issues aside, can somebody actually help me solve the problem.

If the vector class has a method called empty() then when I do it
eg
itBestRect.empty()

well, pressing the . brings up some wierd methods I have never seen before and does not bring up and empty() method as a possible option.
examples:
itBestRect._Adopt
itBestRect._Compat
itBestRect._Getmycont

What's with these? That's why I want to setup visual studio 8 with unmanaged code or otherwise known as native code. Because I don't want any of the fluff microsoft has added.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by utilae
Spelling issues aside, can somebody actually help me solve the problem.

If the vector class has a method called empty() then when I do it
eg
itBestRect.empty()

well, pressing the . brings up some wierd methods I have never seen before and does not bring up and empty() method as a possible option.
examples:
itBestRect._Adopt
itBestRect._Compat
itBestRect._Getmycont

What's with these? That's why I want to setup visual studio 8 with unmanaged code or otherwise known as native code. Because I don't want any of the fluff microsoft has added.
itBestRect is an iterator, not a vector. It doesn't have an empty() member.

You can call std::vector::empty() to see if a vector has zero elements in it, and you can check a std::vector::iterator against it's owning vector's std::vector::end() to see if it's the end of the container (I.e. an invalid iterator). It's not valid to check it against null - that may have worked if VC2003 used pointers instead of iterators (As some STL implementations do for std::vector), but that just means that VC2003 was letting you use incorrect code.

As for those members like _Adopt; they're used internally. Just ignore them and any other members prefixed with an underscore.

Share this post


Link to post
Share on other sites
utilae    188
Hi thanks.

You are right.
In my focus on VS2008 and its settings, I forgot that I was for some reason thinking an iterator had an empty function.

Share this post


Link to post
Share on other sites
utilae    188
One question.

How do I make sure that an iterator to a vector is valid when reading from one.
eg
if(itBestRect is valid, ie is pointing to an element in the vector)
{
RECT recSrcHole;
recSrcHole.top=itBestRect->top;
recSrcHole.left=itBestRect->left;
recSrcHole.right=itBestRect->right;
recSrcHole.bottom=itBestRect->bottom;
}

Thanks

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by utilae
One question.

How do I make sure that an iterator to a vector is valid when reading from one.
eg
if(itBestRect is valid, ie is pointing to an element in the vector)
{
RECT recSrcHole;
recSrcHole.top=itBestRect->top;
recSrcHole.left=itBestRect->left;
recSrcHole.right=itBestRect->right;
recSrcHole.bottom=itBestRect->bottom;
}

Thanks
You'll need to set it to an invalid value to start with. For example:

vector<RECT>::iterator itSearchRect=lstFilledRects.begin();
vector<RECT>::iterator itBestRect=lstFilledRects.end(); // Set to invalid value here
int nSrcRectMaxBottom=0, nSrcRectMinLeft=recContainer.right;
while(itSearchRect!=lstFilledRects.end())
{
// Blah
}

if(itBestRect != lstFilledRects.end())
{
// Iterator isn't pointing at the end of the vector, so it's valid.
}

Share this post


Link to post
Share on other sites
Zahlman    1682
Don't bother fiddling around with iterators for a "high water-mark" algorithm. The standard library already implements that algorithm for you. The only iterators you need to worry about are the endpoints of the search region, and the result.


bool compareRects(const RECT& a, const RECT& b) {
// return true if b is "better" than a, and false otherwise
}

vector<RECT>::iterator itBestRect = std::max_element(lstFilledRects.begin(), lstFilledRects.end(), compareRects);

if (itBestRect == lstFilledRects.end()) {
// The "best" rectangle could not be found; but the only way that can happen
// is if lstFilledRects is empty.
} else {
// go ahead and do something with the "best" rectangle.
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this