Why can't I sort this container?

Posted by Knowing me knowing you on Stack Overflow See other posts from Stack Overflow or by Knowing me knowing you
Published on 2010-04-23T19:04:37Z Indexed on 2010/04/23 19:13 UTC
Read the original article Hit count: 174

Filed under:
|

Please don't mind that there is no insert fnc and that data are hardcoded. The main purpouse of it is to correctly implement iterator for this container.

//file Set.h
#pragma once

template<class T>
class Set
{
    template<class T>
    friend ostream& operator<<(ostream& out, const Set<T>& obj);
private:
    T** myData_;
    std::size_t mySize_;
    std::size_t myIndex_;
public:

    Set();
    class iterator : public std::iterator<std::random_access_iterator_tag, T*>
    {
    private:

        T** itData_;

    public:

        iterator(T** obj)
        {
            itData_ = obj;

        }

        T operator*() const
        {
            return **itData_;
        }
        /*Comparing values of two iterators*/
        bool operator<(const iterator& obj)
        {
            return **itData_ < **obj.itData_;
        }

        /*Substracting two iterators*/
        difference_type operator-(const iterator& obj)
        {
            return itData_ - obj.itData_;
        }

        /*Moving iterator backward for value*/
        iterator operator-(const int value)
        {
            return itData_ - value;
        }

        /*Adding two iterators*/
        difference_type operator+(const iterator& obj)
        {
            return itData_ + obj.itData_;
        }

        /*Moving iterator forward for value*/
        iterator operator+(const int value)
        {
            return itData_ + value;
        }

        bool operator!=(const iterator& obj)
        {
            return (itData_ != obj.itData_);
        }

        bool operator==(const iterator& obj)
        {
            return (itData_ == obj.itData_);
        }

        T** operator++()
        {
            return ++itData_;
        }

        T** operator--()
        {
            return --itData_;
        }
    };

    iterator begin() const
    {
        return myData_;
    }

    iterator end() const
    {
        return myData_ + myIndex_;
    }
};

template<class T>
ostream& operator<<(ostream& out, const Set<T>& obj)
{
    for (int i = 0;i < 3; ++i)
    {
        out << *obj.myData_[i] << "\n";
    }
    return out;
}

//file Set_impl.h
#pragma once
#include "stdafx.h"
#include "Set.h"

template<class T>
Set<T>::Set()
{
    mySize_ = 3;
    myIndex_ = 3;
    myData_ = new T*[mySize_];
    myData_[0] = new T(3);
    myData_[1] = new T(1);
    myData_[2] = new T(2);
}

//main
include "stdafx.h"
#include "Set_impl.h"

int _tmain(int argc, _TCHAR* argv[])
{
    Set<int> a;
    Set<int>::iterator beg_ = a.begin();
    Set<int>::iterator end_ = a.end();
    std::sort(beg_,end_);//WONT SORT THIS RANGE
    cin.get();
    return 0;
}

Why sort can't accept this iterators even though I've provided all operators needed for sort to work? I think the best way to check what's going on is to paste this code and run it first. Thanks

© Stack Overflow or respective owner

Related posts about c++

Related posts about iterator