3

私は次のコードを持っています:

 return new std::pair<BST<Data>::iterator(cursor), true>;

これにより、次のエラーが発生します。

'(operator new(4u)、(、((int *))))'を'int*'から'std :: pair、bool>'に変換できませんでした'の
テンプレートパラメータリストの引数1で型/値の不一致テンプレートstructstd:: pair '

ここで何が問題になるのでしょうか?

4

2 に答える 2

3

Apart from the new (don't use new unless you have to) and return, in order to construct a pair, use either the mentioned make_pair() function or invoke the constructor like this: pair<T1, T2>(v1, v2). You were mixing up the type (pair<T1, T2>) with the values to init that type's instance (v1, v2).

于 2013-01-23T06:09:30.383 に答える
2

何を返そうとしていますか?値によるペアですか、それとも新しいオブジェクト ペアへのポインタですか? 関数の宣言で戻り値の型を確認すると、意図を知ることができます。

ペアを返そうとする場合は、次を使用することをお勧めします。

template <class T1,class T2>
  pair<T1,T2> make_pair (T1 x, T2 y)
  {
    return ( pair<T1,T2>(x,y) );
  }

つまり、次のようなものです。

return  std::make_pair ( BST<Data>::iterator(cursor),  true);

または直接:

return ( pair<T1,T2>(x,y) );

つまり、次のようなものです。

return ( std::pair< BST<Data>::iterator , bool>( cursor, true) );

新しく作成されたオブジェクトへのポインターが必要な場合は、次を使用します。

return ( new std::pair< BST<Data>::iterator , bool>( cursor, true) );

今:

ここで何が問題になる可能性がありますか?

見つめている:

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) { }
};

T1型とが必要な値を使用してテンプレートをインスタンス化しようとしていますT2

于 2013-01-23T08:12:52.043 に答える