use push_pack for vector pair

I am trying to build a function that take a transfer_stops which is a vector of Busstop* and return visit_transfers which is a pair of Visit_stop*, (visit_stop was spicified before as:
typedef pair<Busstop*,double> Visit_stop)

so the first of visit_transfers has to be transfer_stops.

1
2
3
4
5
6
7
8
9
vector <Visit_stop*> Busline::get_tr_stops()
{
    vector <Visit_stop*> visit_transfers;
    for(vector<Busstop*>::iterator tr_stops= transfer_stops.begin(); tr_stops<   transfer_stops.end(); ++tr_stops)
	{
	   visit_transfers.push_back(make_pair(*tr_stops,0.0));
	}
	return (visit_transfers);
}


But I got an error in using the push_back :

src\busline.cpp(201): error C2664: 'void std::vector<_Ty>::push_back(std::pair<_Ty1,_Ty2> *&&)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> *&&'
1> with
1> [
1> _Ty=Visit_stop *,
1> _Ty1=Busstop *,
1> _Ty2=double
1> ]
1> and
1> [
1> _Ty1=Busstop *,
1> _Ty2=double
1> ]
1> and
1> [
1> _Ty1=Busstop *,
1> _Ty2=double
1> ]
1> Reason: cannot convert from 'std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> *'
1> with
1> [
1> _Ty1=Busstop *,
1> _Ty2=double
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

How could I build a vector of visit_stop* that the first element (Busstop*) are taken from a vector of Busstop*??

Thanks a lot,
Hend
Hi,

Instead of creating visit_transfers inside the function, send it as an argument by reference, and make the function void.

Also consider using a range based for loop instead of the iterators.

With the make_pair , it returns a type std::pair<type a, type b> with the types deduced from the argument sent to it. But the expected return type should be a pointer. Consider using emplace_back instead.
Hi,
Thanks for answering me.
I change the code to be:
1
2
3
4
5
6
7
void Busline::get_tr_stops(vector <Visit_stop*> visit_transfers)
{
	for(size_t i=0; i<transfer_stops.size(); ++i)
	{
		visit_transfers.emplace_back(transfer_stops[i] , 0.0);
	}
}


But I got errors:
C:\Program Files (x86)\Microsoft Visual Studio 12\VC\include\xmemory0(617): error C2440: 'initializing' : cannot convert from 'double' to 'std::pair<_Ty1,_Ty2> '
1> with
1> [
1> _Ty1=Busstop *,
1> _Ty2=double
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12\VC\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,Busstop*&,double>(_Objty *,_V0_t,_V1_t &&)' being compiled
1> with
1> [
1> _Ty=std::pair<Busstop *,double> *,
1> _Objty=std::pair<Busstop *,double> *,
1> _V0_t=Busstop *&,
1> _V1_t=double
1> ]
1> C:\Program Files (x86)\Microsoft Visual Studio 12\VC\include\xmemory0(751) : see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,Busstop*&,double>(_Objty *,_V0_t,_V1_t &&)' being compiled
1> with
1> [
1> _Ty=std::pair<Busstop *,double> *,
1> _Objty=std::pair<Busstop *,double> *,
1> _V0_t=Busstop *&,
1> _V1_t=double
1> ]
Sorry for making careless mistakes in the code, because I am new in C++

Thanks a lot,
Hi,

Sorry I didn't know you were new to C++, your code is reasonably advanced for a beginner :+)

Does the Visit_stop class have a constructor which takes those arguments? That's what emplace_back does - calls a constructor.

I gather transfer_stops is a member variable.

Pass by reference, otherwise no values are changed in visit_transfers:

void Busline::get_tr_stops(std::vector<Visit_stop*>& visit_transfers)

Are you doing polymorphism? If not, won't need pointers. Pointers seem to be the problem at the moment

Maybe we should see all of your relevant header and cpp files? Preferably something minimal we can compile ourselves :+)

By range based for loop, I meant this:

http://en.cppreference.com/w/cpp/language/range-for

Topic archived. No new replies allowed.