c2679: Not sure what to do.

Started by
3 comments, last by nobodynews 16 years, 2 months ago
I'm upgrading code from Visual Studio 6 to Visual Studio 2005 and I'm absolutely stumped on what the compiler is complaing about. C2679. I've been reviewing the code over and over. error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Position *' Not exactly sure how to overload the vector '=' operator either and hopefully I can just fix the bug in the code that gives me the error. Here it is: void Graphics::RemoveDuplicatePositions(){ Vector<Position>:: pos_it; int vec_size = positions.size(); for(int i = 0; i < vec_size && vec_size != 1) { pos_it = &positions; //ERROR HERE: C2679 'Position *' if(positions == positions[(i + 1) % vec_size]) { positions.erase(pos_it); vec_size = positions.size(); } else { i++; } }//end loop } What do I need to do to fix this problem? (I wish I could copy and paste this but, I'm typing it by hand from referring to the computer that has Visual Studio yet no internet connection)
Advertisement
Use [ source ] tags.

You want:
pos_it = positions.begin() + i;

Pointers are a kind of iterator, but nowhere in the C++ standard does it say you can create a vector<T>::iterator from an arbitrary pointer, like your current code does. Since VS6 takes the shoddy route of just making vector::iterators actual pointers to it's internal buffer, it compiles your code. VS2005 provides nice features like debug iterators, and stiffens up what kind of code it will accept, which is arguably a good thing.
Thanks! That did the trick!
The error is pretty obvious, its saying that you can't make an iterator from a pointer. Which is true. Fortunately, with random access iterators one can make an iterator from a begin() position and an index very intuitively:
vec.begin() + index;


For a non-random access, it can still be done but ain't so pretty:
std::vector<SomeType>::iterator it = vec.begin();std::advance(it,index);


However, there may be a better way. It appears you are trying to remove duplicates from a sorted container. Then, perhaps consider std::unique? Remember the golden rule: if it seems like a common task, someone's already written it for you:
void Graphics::RemoveDuplicatePositions(){    positions.erase(std::unique(positions.begin(),positions.end()),positions.end());}
What is Vector? It doesn't look like std::vector to me and if it isn't then we don't know what pos_it is. But you're missing an identifier there so who knows? Actually, if you're totally against 'duplicate positions' there's a chance there are better containers for the job like std::set (if you don't care about order). Also, you're code will miss duplicates if it is not ordered. My best guess is that if you used std::vector in VC++ 6.0 that MS's implementation was that the iterator was just a normal pointer instead of a wrapper. Additionally, the size changes when you erase an element yet you do not take that into account in your code.

Right now it's hard to give advice without knowing more about positions. Do you want no duplicates ever? Does order matter? If the answers are yes and no then check out std::set.

edit: I forgot about std::unique

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement