Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compile error problem...

Status
Not open for further replies.

Kanaric

Programmer
Joined
Oct 1, 2005
Messages
1
Location
US
I have a program to do for my data structures class and I have a problem that uses the "vector" library. Basically it wants me to write a template that reverses the elements in the vector object. Also it states the prototype it wants me to use... heres what I have so far.

#include <iostream>
#include <vector>
using namespace std;

template<class elemtype>
void reverseVector(vector<elemtype> &list)
{
elemtype b;
elemtype a;
a = list.back();
while (list.front() != a)
{
b=list.front();
list.push_back(b);
list.erase(0);
}
}
I constantly get this error:
c:\documents and settings\administrator.unimatrix001.000\my documents\c++\assign1.cpp(16) : error C2664: 'std::_Vector_iterator<_Ty,_Alloc> std::vector<_Ty>::erase(std::_Vector_iterator<_Ty,_Alloc>)' : cannot convert parameter 1 from 'int' to 'std::_Vector_iterator<_Ty,_Alloc>'
with
[
_Ty=int,
_Alloc=std::allocator<int>
]
No constructor could take the source type, or constructor overload resolution was ambiguous

I have no clue about this error. It says it can't convert from int to 'std::_Vector_iterator<_Ty,_Alloc>' when the erase parameter HAS to be of int. Their is no that in fact WOULDNT be an int. I simply do not get this....
 
erase requires either one or two iterators: it is moaning about erase(0).

The code does not reverse the vector: it just forms it again in the same order.

Your code also has the problem of not reversing correctly if there are duplicates. For instance, if the vector is

1 2 4 7 4

you will only get

4 7 4 1 2

Try using iterators to go through instead of the actual contents: that way you will be independent of duplicates.
 
Try using a reverse_iterator with push_back. Start from rbegin() + 1 and stop at rend().
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top