可能重複:
no matching function error using template template parameters in a function錯誤編譯「敵不過==操作符」
好mornig我不明白,因爲我獲得通過實施全球功能操作編譯錯誤==(在類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;
}
在此先感謝
另外,您可能要考慮在1參數構造函數中使用explicit關鍵字。 – user673679