Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 重写专用子类中的模板函数_C++_Templates_Inheritance_Template Specialization - Fatal编程技术网

C++ 重写专用子类中的模板函数

C++ 重写专用子类中的模板函数,c++,templates,inheritance,template-specialization,C++,Templates,Inheritance,Template Specialization,我有一个模板化的类MatchBase,它有一个运算符==的函数 template<typename Element> class MatchBase{ virtual bool operator ==(const MatchBase<Element>& m) const{ if(_v1 == m.getFirst() && _v2 == m.getSecond()){ return true; } r

我有一个模板化的类MatchBase,它有一个运算符==的函数

template<typename Element>
class MatchBase{
    virtual bool operator ==(const MatchBase<Element>& m) const{
    if(_v1 == m.getFirst() && _v2 == m.getSecond()){
        return true;
    }
    return false;
}
我也试过了

virtual bool operator ==(const betterGraph::MatchBase<graphmatch::Place>& m) const{
    if(_v1.mass_center == m.getFirst().mass_center && _v2.mass_center == m.getSecond().mass_center){
        return true;
    }
    return false;
}
因为它试图从基类编译方法


我有没有办法在子类中重写基类的这个函数?我已经读过这个问题,但这里是在我的类被专门化时被专门化的方法,所以我不知道如何做一个前向声明:/。

函数可能是虚拟的,但在继承基类时它仍然被初始化。 这是很重要的,因为您可能会这样写:

MatchBase<Place> test = Match(p1,p2);
另一种方法可能是在此上下文中专门化
std::less
。因此,您不需要将其作为模板参数传递

template<typename Element>
class MatchBase {

protected:
    Element _v1;
    Element _v2;

public:
    MatchBase(const Element& v, const Element& vv) : _v1(v), _v2(vv)
    {}

    virtual bool operator ==(const MatchBase<Element>& m) const {
        std::less<Element> less;
        bool v1Equal = !less(_v1, m.getFirst()) && !less(m.getFirst(), _v1);
        bool v2Equal = !less(_v2, m.getSecond()) && !less(m.getSecond(), _v2);
        return v1Equal && v2Equal;
    }

    const Element& getFirst() const { return _v1; }
    const Element& getSecond() const { return _v2; }

};

struct Place
{
    int mass_center;
};

template<>
struct std::less<Place>
{
    bool operator()(const Place& p1, const Place& p2) 
    {
        return p1.mass_center < p2.mass_center; 
    };
};

class Match : public MatchBase <Place>
{
public:
    Match(const Place& v, const Place& vv) :
        MatchBase<Place>(v, vv)
    {};
};

模板化
std::less
是否与为
Place
编写方法
=
相同。我希望避免用户需要编写更多的代码。我认为为Place实现
==
最终是最符合逻辑的:)。谢谢你的邀请explaination@Malc我添加了另一个关于如何强制用户实现operator==的“解决方案”。
MatchBase<Place> test = Match(p1,p2);
template<typename Element, typename Less = std::less<Element>>
class MatchBase {

protected:
    Element _v1;
    Element _v2;

public:
    MatchBase(const Element& v, const Element& vv) : _v1(v), _v2(vv)
    {}

    virtual bool operator ==(const MatchBase<Element, Less>& m) const {
        Less less;
        bool v1Equal = !less(_v1, m.getFirst()) && !less(m.getFirst(), _v1);
        bool v2Equal = !less(_v2, m.getSecond()) && !less(m.getSecond(), _v2);
        return v1Equal && v2Equal;
    }

    const Element& getFirst() const { return _v1; }
    const Element& getSecond() const { return _v2; }

};

struct Place
{
    int mass_center;
};

struct PlaceLess
{
    bool operator()(const Place& p1, const Place& p2) 
    {
        return p1.mass_center < p2.mass_center; 
    };
};

class Match : public MatchBase <Place, PlaceLess>
{
public:
    Match(const Place& v, const Place& vv) :
        MatchBase<Place, PlaceLess>(v, vv)
    {};
};
template<typename Element>
class MatchBase {

protected:
    Element _v1;
    Element _v2;

public:
    MatchBase(const Element& v, const Element& vv) : _v1(v), _v2(vv)
    {}

    virtual bool operator ==(const MatchBase<Element>& m) const {
        std::less<Element> less;
        bool v1Equal = !less(_v1, m.getFirst()) && !less(m.getFirst(), _v1);
        bool v2Equal = !less(_v2, m.getSecond()) && !less(m.getSecond(), _v2);
        return v1Equal && v2Equal;
    }

    const Element& getFirst() const { return _v1; }
    const Element& getSecond() const { return _v2; }

};

struct Place
{
    int mass_center;
};

template<>
struct std::less<Place>
{
    bool operator()(const Place& p1, const Place& p2) 
    {
        return p1.mass_center < p2.mass_center; 
    };
};

class Match : public MatchBase <Place>
{
public:
    Match(const Place& v, const Place& vv) :
        MatchBase<Place>(v, vv)
    {};
};
template <typename T>
struct IComparable
{
    virtual bool operator==(const T& other) const = 0;
};


template<typename Element>
class MatchBase {

    static_assert(std::is_base_of<IComparable<Element>, Element>::value, "Element must implement comparable");

protected:
    Element _v1;
    Element _v2;

public:
    MatchBase(const Element& v, const Element& vv) : _v1(v), _v2(vv)
    {}

    virtual bool operator ==(const MatchBase<Element>& m) const {
        return _v1 == m._v1 && _v2 == m._v2;
    }
};

struct Place : public IComparable<Place>
{
    int mass_center;

    bool operator==(const Place& other) const
    {
        return mass_center == other.mass_center;
    };
};

class Match : public MatchBase <Place>
{
public:
    Match(const Place& v, const Place& vv) :
        MatchBase<Place>(v, vv)
    {};
};