2012-03-29 18 views
2

Ich habe den folgenden Code vorgestellt. Der Compiler löst Fehler aus, wenn ich einen überladenen Postfix-Operator überlasten. Es funktioniert gut bei einem überladenen Präfix-Operator. Fehlerostream operator überladen auf einem überladen Postfix Inkrement/Dekrement-Operator

error: no match for ‘operator<<’ in ‘std::cout << cDigit.Digit::operator++(0)’ 

-Code

#include <iostream> 

using namespace std; 

class Digit 
{ 
private: 
    int m_nDigit; 
public: 
    Digit(int nDigit=0) 
    { 
     m_nDigit = nDigit; 
    } 

    Digit& operator++(); // prefix 
    Digit& operator--(); // prefix 

    Digit operator++(int); // postfix 
    Digit operator--(int); // postfix 

    friend ostream& operator<< (ostream &out, Digit &digit); 

    int GetDigit() const { return m_nDigit; } 
}; 

Digit& Digit::operator++() 
{ 
    // If our number is already at 9, wrap around to 0 
    if (m_nDigit == 9) 
     m_nDigit = 0; 
    // otherwise just increment to next number 
    else 
     ++m_nDigit; 

    return *this; 
} 

Digit& Digit::operator--() 
{ 
    // If our number is already at 0, wrap around to 9 
    if (m_nDigit == 0) 
     m_nDigit = 9; 
    // otherwise just decrement to next number 
    else 
     --m_nDigit; 

    return *this; 
} 

Digit Digit::operator++(int) 
{ 
    // Create a temporary variable with our current digit 
    Digit cResult(m_nDigit); 

    // Use prefix operator to increment this digit 
    ++(*this);    // apply operator 

    // return temporary result 
    return cResult;  // return saved state 
} 

Digit Digit::operator--(int) 
{ 
    // Create a temporary variable with our current digit 
    Digit cResult(m_nDigit); 

    // Use prefix operator to increment this digit 
    --(*this);    // apply operator 

    // return temporary result 
    return cResult;  // return saved state 
} 

ostream& operator<< (ostream &out, Digit &digit) 
{ 
    out << digit.m_nDigit; 
    return out; 
} 

int main() 
{ 
    Digit cDigit(5); 
    cout << ++cDigit << endl; // calls Digit::operator++(); 
    cout << --cDigit << endl; // calls Digit::operator--(); 
    cout << cDigit++ << endl; // calls Digit::operator++(int); //<- Error here?? 
return 0; 
} 

Antwort

6

Ihre operator<< sollte seine Digit Parameter durch konstante Referenz nehmen:

ostream& operator<< (ostream &out, const Digit &digit) 

Dies wird hier benötigt, da Digit::operator++(int) ein temporäres Objekt zurückgibt, die nicht sein kann an eine Funktion übergeben, die eine nicht konstante Referenz verwendet.

+0

+1 Yup das war es! Ich bin überrascht, dass die meisten C++ - Tutorials dies während des Überladens des Operators nicht erwähnen. – enthusiasticgeek

+0

Hrm, vielleicht müssen wir ein paar bessere Tutorials schreiben :-) Eine gute Faustregel ist, wenn Sie nicht die Absicht haben, es zu modifizieren, machen Sie es const. – bstamour

+0

Ich würde schlauere Compiler lieben, die auf Anfänger/Fortgeschrittene Programmierer mit einem Fehler und Vorschlag wie "Herr/Ma'am haben Sie const Qualifier vergessen?" eher als ein Sammelsurium von ungerechtfertigter Komplexität (glücklicherweise nicht so sehr in diesem Fall). Hoffentlich würden wir sie in Zukunft sehen. :) – enthusiasticgeek