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

正如 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>::iteratormyList<float>::iterator- 进行检查,您应该会收到编译器错误。

查看 Barton-Nackman 技巧了解更多详情。

于 2012-12-16T13:29:36.470 回答
0

编译器不能推导出T。改成:

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

第二个模板参数执行简单的 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 回答