khirod
khirod

Reputation: 345

Operator Overloading not working even when both postfix and assignment are overloaded

This is the class definition

template <typename key_type, typename mapped_type>
class mymap {
    public:
    node<key_type, mapped_type> *root;

    /* Utility Functions */
    int height(node<key_type, mapped_type> *);
    int getBalance(node<key_type, mapped_type> *);

    node<key_type, mapped_type> *newNode(key_type, mapped_type);
    node<key_type, mapped_type> *rightRotate(node<key_type, mapped_type> *);
    node<key_type, mapped_type> *leftRotate(node<key_type, mapped_type> *);
    node<key_type, mapped_type> *insert_node(node<key_type, mapped_type> *, key_type, mapped_type);
    node<key_type, mapped_type> *minValueNode(node<key_type, mapped_type> *);
    node<key_type, mapped_type> *deleteNode(node<key_type, mapped_type> *, key_type);

    void insert(key_type, mapped_type);

    // Constructor
    mymap()
    {
    root = NULL;
    }


    class iterator {
        public:

        node<key_type,mapped_type> *ptr;

        iterator(){
            ptr = NULL;
        }
        /*void iterator(iterator &x) {
            ptr = x.ptr;

        }*/

        void operator++(int) {                
            ptr = increment(ptr); 
        }

        void operator=(const iterator &it) {
            ptr = it.ptr;
        //return (*this);
        }


        /*void  operator--(int) {
            ptr = decrement(ptr); 
        }*/

        void operator++() {
            ptr = increment(ptr); 
        }

        /*
        node <key_type,mapped_type>* operator--() {
            return *(decrement(ptr));
        }

        bool operator !=(iterator itr) {
            return (ptr != itr.ptr);
        }


        bool operator ==(iterator itr) {
            return (ptr == itr.ptr);
        }
        */  
    };

    iterator begin() {

        iterator it ;
        node<key_type,mapped_type> * tmp = root;

        while (tmp->left != NULL)
            tmp = tmp->left;    

        it.ptr = tmp;
        return it;
    }

    iterator end() {
        iterator it;
        it.ptr = NULL;
        return it;
    }

};

In main this code won't compile and gives error at line 10. Maybe because I'm trying to do something wrong while overloading postfix++ and assignment operators.

mymap<int, int> A;

A.insert(1, 5);
A.insert(2, 5);
A.insert(3, 5);
A.insert(4, 5);

mymap<int,int> :: iterator it = A.begin();
mymap<int,int> :: iterator it1 = A.end();
it1 = it++;

Plz help :(. This is an AVL implementation of map I'm trying to accomplish. This is the link to the complete map file if this is necessary. https://www.box.com/s/oom4bjnve9zmlpdcevip

Upvotes: 0

Views: 146

Answers (2)

it1 = it++;

This will evaluate the postincrement of your iterator, and use the return type (in your declaration is void) to initialize it1, which is a compiler error. Note that, when overloading operators you should do as ints do. I.e. Try to mimic the behavior of the operators in existing types to avoid confusion. That includes having operator= returning a reference to the object that has been assigned.

Upvotes: 1

IronMensan
IronMensan

Reputation: 6831

You have the wrong return types. Neither prefix nor postfix operators return void. Prefix should return an iterator & and postfix should return a iterator instance.

For reference: http://msdn.microsoft.com/en-US/library/f6s9k9ta%28v=VS.80%29.aspx

Upvotes: 1

Related Questions