lundi 25 juin 2018

Why I get the following error: "no match for 'operator=='" ? (nasted class with template)

given the following code:

template<class T>
class TemplateClass {
    T val;
public:
    TemplateClass(T val) :
            val(val) {
    }
    TemplateClass(const TemplateClass& tc) = default;
    TemplateClass& operator=(const TemplateClass& tc) = default;

    class Ele {
        T x;
    public:
        Ele(T x) :
                x(x) {
        }
        template<class S>
        friend bool operator==(const typename TemplateClass<S>::Ele& e1,
                const typename TemplateClass<S>::Ele& e2);
    };
};

template<class T>
bool operator==(const typename TemplateClass<T>::Ele& e1,
        const typename TemplateClass<T>::Ele& e2) {
    return e1.x == e2.x;
}

int main() {
    TemplateClass<int>::Ele e1(4);
    TemplateClass<int>::Ele e2(3);
    if (e1 == e2) {  // *********** error
        std::cout << "ok" << std::endl;
    }
}  

I get the following error:

no match for 'operator==' (operand types are 'TemplateClass::Ele' and 'TemplateClass::Ele')

Can someone tell me how can I fix it and what is the problem?

Aucun commentaire:

Enregistrer un commentaire