Operatoren überladen

sammy2ooo

voll Tohuwabohu
Warum funktioniert folgendes nicht??

functions.cpp
Code:
...
// wert fuer wert multiplizieren
std::vector<double> operator*(std::vector<double>& a, std::vector<double>& b) {
        std::vector<double> c;

        for(std::vector<double>::const_iterator ita = a.begin(), itb = b.begin(); ita < a.end(), itb < b.end(); ita++, itb++) {
                c.push_back((*ita)*(*itb));
        }

        return c;
}

// Skalar mit einem Vektor multiplizieren
std::vector<double> operator*(double& a, std::vector<double>& b) {
        std::vector<double> c;

        for(std::vector<double>::const_iterator it = b.begin(); it < b.end(); it++) {
                c.push_back((*it)*a);
        }

        return c;
}

// Skalarprodukt von zwei Vektoren
double operator*(std::vector<double>& a, std::vector<double>& b) {
        double temp = 0;

        for(std::vector<double>::const_iterator ita = a.begin(), itb = b.begin(); ita < a.end(), itb < b.end(); ita++, itb++) {
                temp = temp + *ita * *itb;
        }

        return temp;
}
...

functions.h
Code:
...
std::vector<double> operator*(std::vector<double>&, std::vector<double>&);
std::vector<double> operator*(double&, std::vector<double>&);
double operator*(std::vector<double>&, std::vector<double>&);
...

ich bekomm da ne Fehlermeldung aus der ich nicht ganz schlau werde.

sammy@uranos > make
building functions.o
functions.h:21: error: new declaration 'double operator*(std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)'
functions.h:19: error: ambiguates old declaration 'std::vector<double, std::allocator<double> > operator*(std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)'
functions.cpp: In function 'std::vector<double, std::allocator<double> > operator*(std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)':
functions.cpp:80: error: new declaration 'std::vector<double, std::allocator<double> > operator*(std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)'
functions.h:21: error: ambiguates old declaration 'double operator*(std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)'
functions.cpp: In function 'double operator*(std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)':
functions.cpp:102: error: new declaration 'double operator*(std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)'
functions.cpp:80: error: ambiguates old declaration 'std::vector<double, std::allocator<double> > operator*(std::vector<double, std::allocator<double> >&, std::vector<double, std::allocator<double> >&)'
make: *** [functions.o] Error 1

Die Signaturen sind doch unterschiedlich, also müsste der Kompiler doch zwischen den Überladungen unterscheiden können...

Grüsse,
Matze
 
Ich glaube der Rückgabewert ist nicht Teil der Signatur einer Funktion. Für C++ sind deshalb die erste und die letzte Funktion in "functions.cpp" identisch, weil sie sich nur im Rückgabewert unterscheiden.


Außerdem sind die Fehlermeldungen von C++ nicht dazu da, Hinweise auf das wirkliche Problem zu geben, darum kann man auch nicht erwarten daraus schlau zu werden. ;)
 
Ich glaube der Rückgabewert ist nicht Teil der Signatur einer Funktion. Für C++ sind deshalb die erste und die letzte Funktion in "functions.cpp" identisch, weil sie sich nur im Rückgabewert unterscheiden.

öhm... also der Rückgabewert gehört schon zur Signatur dazu... der Kompiler müsste doch anhand des Datentypes auf der linken Seite merken welche Funktion zu benutzen ist.
 
Nein.

Die beiden Funktionen

Code:
int foo(void);
double foo(void);

erzeugen einen Konflikt, aber

Code:
int foo(void);
int foo(int);

ist erlaubt. Rückgabewerte können jederzeit weggeworfen werden, der Compiler könnte im ersten Beispiel nicht entscheiden, welche Funktion beim Aufruf von

Code:
foo();

gemeint ist. Im zweiten Beispiel ist dies aber leicht zu entscheiden.
 
danke fuer die Antworten. Eine Frage habe ich noch zu default-values bei Parametern, wollte deswegen keinen neuen Thread aufmachen...

hier der funktionskopf

functions.cpp
double diff(double x, double delta=0.000001) {...}

functions.h
double diff(double, double);
// double diff(double); <==== so schaut das fuer mich eigentlich am besten aus, geht aber auch nicht...

main.cpp
diff(4.3)

nur motzt der compiler trotzdem staendig ueber zuwenig parameter
functions.h:21: error: too few arguments to function 'double diff(double, double)'
 
Zurück
Oben