Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it not using my overloaded operator for ++?

I have created an overloaded operator for ++ in my .hpp file and then a function which calls it also in the .hpp file. The .cpp file then calls the function but when the function hit the ++ code it doesnt eneter the overloaded operator but instead used the default operation operator for ++. Why isnt it using my overloaded operator?

Here is the operators for ++:

iterator& operator ++ () {  // pre-increment
        std::list<value_type>::iterator i = listOfValues.begin();
        advance(i,1);
        return &*i;
    }

    Square_List operator ++ (int) { // post-increment
        std::list<value_type>::iterator i = listOfValues.begin();
        advance(i,1);
        return &*i;
    }

And here is the function using it:

template <typename A_>
void testerIncrement(A_ toInc)
{

    toInc = listOfValues.begin();
    cout << "\n" << *toInc;
    ++toInc;
    cout << "\n" << *toInc;
    ++toInc;
}

And lastly the call for the function in the .cpp file

ca.testerIncrement(ca.listOfValues.begin());
like image 675
Beef Avatar asked Oct 08 '22 15:10

Beef


1 Answers

You overload the prefix increment operator ++ with either a nonmember function operator that has one argument of class type or a reference to class type, or with a member function operator that has no arguments.

It seems you are using non member operators. So your syntax should be void operator++(iterator &); //prefix and void operator++(Square_List, int); //postfix.

like image 162
Sharad Avatar answered Oct 12 '22 09:10

Sharad