简体   繁体   中英

postfix prefix operator overload error c++

When using operator overloading for prefix and postfix increment, I get an error from compiler:

"Fajl Fajl::operator ++(int)' : member function already defined or declared"

Here are my headers for operator ++:

Fajl& operator ++ (); // prefix
Fajl& operator -- (); // prefix
Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix

And my implementations:

Fajl& Fajl::operator ++ () // prefix
{
    ++(*poz);
    return *this;
}

Fajl& Fajl::operator -- () // prefix
{
    --(*poz);
    return *this;
}

Fajl Fajl::operator ++ (int dummy) // postfix
{
    Fajl temp(*this);
    ++(*this);
    return temp;
}

Fajl Fajl::operator -- (int dummy) // postfix
{
    Fajl temp(*this);
    --(*this);
    return temp;
}

"Fajl" is the class, and "poz" its argument which I'm incrementing. What am I doing wrong?

Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix
              ^^
           should be --

Hard to be sure, but perhaps it's referring to the fact that you've duplicated the declaration for postfix increment:

Fajl operator ++ (int); // postfix
Fajl operator ++ (int); // postfix

Presumably one of those was suppose to be -- instead of ++ . That quite likely leads to another problem: you've apparently defined an operator-- , but it's not declared in the class definition. If it hasn't yet, the compiler will almost certainly complain about that as well.

You have 2 identical declarations of Fajl operator ++ (int); Correct the 2nd one to operator--

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM