2

重複の可能性:
関数でテンプレート テンプレート パラメーターを使用すると、一致する関数エラーがありません

おはようございます。グローバル関数として operator== (クラス myList::iterator 内) を実装するとコンパイル エラーが発生するため、わかりません。operator== をメンバ関数として認識した場合、コードはコンパイルされます。

エラーは

error: no match for operator== in it == it1
note: candidate is:
note: tempalte<class T> bool operator==(const typename myList<T>::iterator&,const typename myList<T>::iterator&)

コードは次のとおりです。

#include <iostream>
#include <cstdlib>
#include <iterator>
#include <typeinfo>

template <class T>
class myList;

template <class T>
bool operator==(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs);
template <class T>
bool operator!=(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs);

template <class T>
class myList
{
private:
  class myInfo;
public:
  //CTR DEFAULT
  myList():_pInfo(NULL)
  {}
  myList(T value):_pInfo(new myInfo(value))
  {}
  //  class iterator;
  //  friend class iterator;
  class iterator{
  public:
    //creo gli iteratori
    iterator():_pMyInfoIt(NULL)
    {}
    iterator(myInfo* p):_pMyInfoIt(p)
    {}
    iterator(const iterator& it):_pMyInfoIt(it._pMyInfoIt)
    {}
    iterator& operator=(const iterator& it)
    {
      _pMyInfoIt = it._pMyInfoIt;
      return *this;
    }
    //creo funzioni che lavorano sugli iteratori
    /*
    bool operator==(const iterator& rhs)
    {
      return _pMyInfoIt == rhs._pMyInfoIt;

    }
    */
    friend bool operator== <T>(const typename myList::iterator& lhs,const typename myList::iterator& rhs);
    friend bool operator!= <T>(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs);
  private:
    myInfo* _pMyInfoIt;
  };

  myList::iterator begin()
  {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
    return iterator(_pInfo);
  }

private:
  class myInfo
  {
  public:
    myInfo(const T& data):_data(data),_pMyInfo(NULL)
    {}
  private:
    T _data;
    myInfo* _pMyInfo;
  };
  myInfo* _pInfo;
};


template <class T>
bool operator==(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs)
{
  std::cout << __PRETTY_FUNCTION__ << std::endl;
  return lhs._pMyInfoIt == rhs._pMyInfoIt;
}

template <class T>
bool operator!=(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs)
{
  return !(lhs == rhs);
}

int main(int argc,char** argv){
  myList<int> test;
  myList<int>::iterator it = test.begin();
  myList<int>::iterator it1 = test.begin();
  std::cout << typeid(it1).name() << std::endl;
  if(it == it1)
    std::cout << "EQUAL" << std::endl;
  return EXIT_SUCCESS;
}

前もって感謝します

4

3 に答える 3

0

コンパイラはTを推測できません。次のように変更します。

template <class IT, class=typename IT::iterator_category>
bool operator==(const IT& lhs, const IT& rhs)

2番目のテンプレートパラメーターは単純なSFINAEを実行するため、イテレーターのみが一致します。

于 2012-12-16T13:00:57.377 に答える
0

クラス本体内でフレンド関数を定義します。

friend bool operator== <>(const typename myList<T>::iterator& lhs, const typename myList<T>::iterator& rhs)
{
    std::cout << __LINE__ << std::endl;
    return lhs._pMyInfoIt == rhs._pMyInfoIt;
}
friend bool operator!= <>(const typename myList<T>::iterator& lhs, const typename myList<T>::iterator& rhs)
{
    return !(lhs == rhs);
}

動作するはずです。

于 2012-12-16T13:01:42.850 に答える
0

pimanych が言うように、クラス本体内でフレンド関数の本体を宣言します。

    friend bool operator==(const iterator& lhs, const iterator& rhs)
    {
        return lhs._pMyInfoIt == rhs._pMyInfoIt;
    }
    friend bool operator!=(const iterator& lhs, const iterator& rhs)
    {
        return !(lhs == rhs);
    }

これらは実際にはテンプレート関数ではなく、myList<T>(and myList<T>::iterator) がインスタンス化されたときにのみ宣言されることに注意してください。myList<int>::iteratorとを比較して確認できmyList<float>::iteratorます - コンパイラ エラーが発生するはずです。

詳細については、Barton-Nackman トリックを参照してください。

于 2012-12-16T13:29:36.470 に答える