可能重复:
在函数中使用模板模板参数时没有匹配函数错误
早上好,我不明白,因为我通过将 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;
}
提前致谢