Error while returning pair in cpp
I have the following code:
return new std::pair<BST<Data>::iterator(cursor), true>;
This leads to the following errors:
failed to convert '(operator new (4u), (, ((int *))))' from 'int *' to 'std :: pair, bool>
type / value mismatch in argument 1 in template parameter list for' template struct std :: pair '
What could be the problem?
What are you trying to get back to? Pair by value, or really a pointer to a new pair of objects? It would be helpful to see the return type in your function declaration to know your intent.
If you are trying to return a pair, you are better off using:
template <class T1,class T2>
pair<T1,T2> make_pair (T1 x, T2 y)
{
return ( pair<T1,T2>(x,y) );
}
That is, something like:
return std::make_pair ( BST<Data>::iterator(cursor), true);
Or directly:
return ( pair<T1,T2>(x,y) );
That is, something like:
return ( std::pair< BST<Data>::iterator , bool>( cursor, true) );
If a pointer to a newly created object, if you want, use:
return ( new std::pair< BST<Data>::iterator , bool>( cursor, true) );
Now:
What could be the problem?
Looking at:
template <class T1, class T2> struct pair
{
typedef T1 first_type;
typedef T2 second_type;
T1 first;
T2 second;
pair() : first(T1()), second(T2()) {}
pair(const T1& x, const T2& y) : first(x), second(y) {}
template <class U, class V>
pair (const pair<U,V> &p) : first(p.first), second(p.second) { }
};
You are trying to instantiate a template using values where we need types T1
and T2
.
source to share