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?

+3


source to share


2 answers


In addition to new

(or use new ones, if you do not need), and return

to build pair

, use a named function make_pair()

or call the constructor as follows: pair<T1, T2>(v1, v2)

. You mixed type ( pair<T1, T2>

) with values ​​to initialize an instance of an instance ( v1, v2

).



+3


source


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

.

+2


source







All Articles