mercredi 4 septembre 2019

Implementing posfixed operator++() for a template class [duplicate]

I'm writing a template class for manage plane angles. I would like to implement the ++() operator in both versions (prefix and postfix).
The data (degrees, minutes and seconds) are stored in a std::tuple. The operators ++() and --() increase or decrease the degrees, leaving the value of the minutes and the seconds unchanged.
The implementation of the prefix operator works, but I have no idea how to write the same operator in the postfix version.
Here is the code in simplified version:

#include <iostream>
#include <tuple>
#include <locale>

using namespace std;

using ushort = unsigned short;
using measure = tuple<short,    // degrees
                      ushort,   // minutes
                      ushort>;  // seconds

template<ushort D, ushort M>
class angle
{
public:
    angle();
    angle(short d, ushort m = 0, ushort s = 0);
    angle& operator++();

    void print();

private:
    measure dms;
};

template<ushort D, ushort M>
angle<D, M>::angle()
{
    dms = make_tuple(0,0,0);
}

template<ushort D, ushort M>
angle<D, M>::angle(short d, ushort m, ushort s)
{
    dms = make_tuple(d, m, s);
}

template<ushort D, ushort M>
void angle<D, M>::print()
{
    cout << get<0>(dms) << '°'  << ' ' <<
            get<1>(dms) << '\'' << ' ' <<
            get<2>(dms) << '\"' << endl;
}

template<ushort D, ushort M>
angle<D, M>& angle<D, M>::operator++()
{
    std::get<0>(dms)++;
    return *this;
}

int main()
{
    setlocale(LC_ALL, "");

    angle<360, 60> a1(30);
    a1.print();
    ++a1;       // all rigth
    a1.print();
    a1++;   //..|error: no 'operator++(int)' declared for 
            // postfix '++' [-fpermissive]|
    a1.print();

    return 0;
}

Compiling with GCC (9.1) in C++11 I get the following error:
no 'operator++(int)' declared for postfix '++' [-fpermissive].
I would prefer not to use the -fpermissive option, but to correctly write the code for the ++() postfix operator. Any suggestions are appreciated

Aucun commentaire:

Enregistrer un commentaire