2016-10-22 4 views
0

Ich folge gerade jetzt ein C++ Coursera-Kurs, versuchen, Beispiel von hier zu kompilieren: Ira Pohl’s C++ by Dissection mit beiden g ++ und Intel C++ Compiler.Mehrdeutige Funktionsüberladung in C++

bekamen einige Fehler mit beiden Compiler, wie dies bei jedem Aufruf zu mehr:

rational.cpp(69): error: "greater" is ambiguous 
     << greater(i, j); 
     ^



/*************************************************************** 
* C++ by Dissection By Ira Pohl   Addison Wesley  
* Chapter 5 Ctors, Dtors, Conversion, and Operator Overloading 
* Compiled with Borland C++ Builder Version 5.0 Summer 2001 
******************************************************************/ 

//Overloading functions 

#include <iostream> 
using namespace std; 


// Overloading functions 

class rational { 
public: 
    rational(int n = 0) : a(n), q(1) { } 
    rational(int i, int j) : a(i), q(j) { } 
    rational(double r) : a(static_cast<long> 
         (r * BIG)), q(BIG) { } 
    void print() const { cout << a << "/" << q; } 
    operator double() 
       { return static_cast<double>(a)/q; } 
    friend ostream& operator<<(ostream& out, const rational& x); 
    friend istream& operator>>(istream& in, rational& x); 
    friend bool operator>(rational w, rational z); 
private: 
    long a, q; 
    enum { BIG = 100 }; 
}; 



ostream& operator<<(ostream& out, const rational& x) 
{ 
    return (out << x.a << "/" << x.q << '\t'); 
} 

istream& operator>>(istream& in, rational& x) 
{ 
    return (in >> x.a >> x.q); 
} 


bool operator>(rational w, rational z) 
{ 
    return (static_cast<double>(w.a)/w.q >   static_cast<double>(z.a)/z.q); 
} 


inline int  greater(int i, int j) 
     { return (i > j ? i : j); } 

inline double greater(double x, double y) 
     { return (x > y ? x : y); } 

inline rational greater(rational w, rational z) 
     { return (w > z ? w : z); } 

int main() 
{ 
    int  i = 10, j = 5; 
    float x = 7.0; 
    double y = 14.5; 
    rational w(10), z(3.5), zmax; 

    cout << "\ngreater(" << i << ", " << j << ") = " 
     << greater(i, j); 
    cout << "\ngreater(" << x << ", " << y << ") = " 
     << greater(x, y); 
    cout << "\ngreater(" << i << ", "; 
    z.print(); 
    cout << ") = " 
     << greater(static_cast<rational>(i), z); 
    zmax = greater(w, z); 
    cout << "\ngreater("; 
    w.print(); 
    cout << ", "; 
    z.print(); 
    cout << ") = "; 
    zmax.print(); 
    cout << endl; 
} 

Antwort

4

Die greater Funktion in den importierten std namespace vorhanden ist, als auch die Quelldatei, so dass der Compiler nicht entscheiden kann, welche man sollte benutzt werden.

Vielleicht war das bei Borland C++ 5.0 nicht der Fall, also kompilierte sich der Code damals gut. Dies ist ein schönes Beispiel, warum using namespace std normalerweise eine schlechte Idee ist.

Sie könnten versuchen, diese Deklaration zu entfernen und manuell ein explizites Präfix std:: wo erforderlich hinzufügen (der Compiler wird es Ihnen sagen).

+0

Sie meinen, es gibt eine 'std :: greater()' Funktion in der 'iostream' Bibliothek? Weil ich es auf den Dokumenten nicht finden kann. –

+0

'größer' scheint nur' funktional' zu existieren, es sei denn, ich vermisse etwas. – Carcigenicate

+0

Gemäß dem angegebenen Link - 'Definiert im Header ' - das OP hier enthält es nicht. –