0

I have a sketch of my own array class and array comparison rule

CArray.h

template <typename T>
    class CArray
    {
    public:

        CArray();
        virtual void setComparisonRule(IArrayComparisonRule<T>* comparisonRule);

    private:

        IArrayComparisonRule<T>* comparisonRule;    
    };

CArray.cpp

template <typename T>
    void CArray<T>::setComparisonRule(IArrayComparisonRule<T>* comparisonRule)
    {
        this->comparisonRule = comparisonRule;
    }

IArrayComparisonRule

template <typename T>
    class IArrayComparisonRule
    {

    public:

        virtual b1 isLeftEqualRight(const T* leftItem, const T* rightItem) = 0;
        virtual b1 isLeftLessThenRight(const T* leftItem, const T* rightItem) = 0;
        virtual b1 isLeftGraterThenRight(const T* leftItem, const T* rightItem) = 0;
        virtual b1 isLeftLessOrEqualThenRight(const T* leftItem, const T* rightItem) = 0;
        virtual b1 isLeftGraterOrEqualThenRight(const T* leftItem, const T* rightItem) = 0;
    };

Array element

class Sample
{
public:
    si32 value;

    Sample(si32 value = 0)
            : value{value}
    {}

    ~Sample()
    {}
};

IArrayComparisonRule implementation

class SampleComparisonRule : public IArrayComparisonRule<Sample>
{

public:

    b1 isLeftEqualRight(const Sample* leftItem, const Sample* rightItem)
    {
        return leftItem->value == rightItem->value;
    }

    b1 isLeftLessThenRight(const Sample* leftItem, const Sample* rightItem)
    {
        return leftItem->value < rightItem->value;
    }

    b1 isLeftGraterThenRight(const Sample* leftItem, const Sample* rightItem)
    {
        return leftItem->value > rightItem->value;
    }

    b1 isLeftLessOrEqualThenRight(const Sample* leftItem, const Sample* rightItem)
    {
        return leftItem->value <= rightItem->value;
    }

    b1 isLeftGraterOrEqualThenRight(const Sample* leftItem, const Sample* rightItem)
    {
        return leftItem->value >= rightItem->value;
    }
};

main.cpp

CArray<Sample*> objects;
objects.setComparisonRule(new SampleComparisonRule());

As you can see I use T as template for both CArray and IArrayComparisonRule.

The compiler (GCC 5.4.0) error is no matching function for call to ‘ee::CArray::setComparisonRule(SampleComparisonRule*)’

When I pass Sample as a pointer to template:

class SampleComparisonRule : public IArrayComparisonRule<Sample*>

template <typename T*>
    class IArrayComparisonRule

the compiler says: expected nested-name-specifier before ‘T’

From my point of view, the problem can be solved if I pass Sample as pointer to IArrayComparisonRule. How can I do that? Maybe there is another way to solve the problem.

0 Answers0