2012-12-16 82 views
2

可能重复:
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; 
} 

在此先感谢

+0

另外,您可能要考虑在1参数构造函数中使用explicit关键字。 – user673679

回答

0

编译器不能推断T.更改为:

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

第二模板参数d简单的SFINAE,所以只有迭代器会匹配。

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

它应该工作。

+0

这不就是'friend bool operator ==(const iterator&lhs ...'如果你这样做的话? – user673679

+0

这些是模板函数,所以它们应该看起来像 friend bool operator!= <>( const typename myList :: iterator&lhs,const typename myList :: iterator&rhs) – pimanych

+0

据我所知,它们不是。它们是正常的非模板函数,由于实例化过程注入全局范围每个myList获取一个 :: iterator)(查找Barton-Nackman技巧)。 – user673679

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>(和myList<T>::iterator)被实例化只是声明。您可以通过尝试比较myList<int>::iteratormyList<float>::iterator进行检查 - 您应该会收到编译器错误。

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