[gelöst] undefined reference to `operator+(EnergySource const&, double)'|
-
Hi, ich vermute mal, ich steh nur auf dem Schlauch, aber ich komm einfach nicht dahinter. Ich wollte die Klasse erst testen, damit ich nciht später im Projekt die Fehler raussuchen muss. Die main.cpp dient nur Testzwecken.
EnergySource.hpp
#ifndef ENERGYSOURCE #define ENERGYSOURCE class EnergySource { private: double MaxVol; double ExtraVol; double CurrVol; public: EnergySource(double Max, double Extra = 0, double Curr = 0) : MaxVol(Max), ExtraVol(Extra), CurrVol(Curr) {} EnergySource(const EnergySource& Orig) : MaxVol(Orig.MaxVol), ExtraVol(Orig.ExtraVol), CurrVol(Orig.CurrVol) {} const EnergySource& operator=(const EnergySource& Orig); const EnergySource& operator+=(double Right); const EnergySource& operator-=(double Right); friend const EnergySource& operator+(const EnergySource& Left, double Right); friend const EnergySource& operator-(const EnergySource& Left, double Right); void SetExtra(double Extra) {ExtraVol = Extra;} void SetCurr(double Curr) {CurrVol = Curr;} double GetMax() {return MaxVol;} double GetExtra() {return ExtraVol;} double GetCurr() {return CurrVol;} }; #endif ENERGYSOURCEEnergySource.cpp
#include "EnergySource.hpp" const EnergySource& EnergySource::operator=(const EnergySource& Orig) { if (this == &Orig) return *this; EnergySource Temp(Orig); double Max = MaxVol; double Extra = ExtraVol; double Curr = CurrVol; MaxVol = Temp.MaxVol; ExtraVol = Temp.ExtraVol; CurrVol = Temp.CurrVol; Temp.MaxVol = Max; Temp.ExtraVol = Extra; Temp.CurrVol = Curr; return *this; } const EnergySource& EnergySource::operator+=(double Right) { CurrVol += Right; if (CurrVol < 0) CurrVol = 0; if (CurrVol > MaxVol + ExtraVol) CurrVol = MaxVol + ExtraVol; return *this; } const EnergySource& EnergySource::operator-=(double Right) { CurrVol -= Right; if (CurrVol < 0) CurrVol = 0; if (CurrVol > MaxVol + ExtraVol) CurrVol = MaxVol + ExtraVol; return *this; } const EnergySource& operator+(EnergySource Left, double Right) { EnergySource Temp(Left); Temp += Right; return Temp; } const EnergySource& operator-(EnergySource Left, double Right) { EnergySource Temp(Left); Temp -= Right; return Temp; }und die main.cpp
#include <iostream> #include "EnergySource.hpp" using namespace std; int main() { EnergySource Testtank(100); cout << "Testtank ist " << Testtank.GetMax() + Testtank.GetExtra() << "l groß und fasst momentan " << Testtank.GetCurr() << "l." << endl; EnergySource Testtank2(200); Testtank2 = Testtank; cout << "Testtank2 ist " << Testtank2.GetMax() + Testtank2.GetExtra() << "l groß und fasst momentan " << Testtank2.GetCurr() << "l." << endl; EnergySource Testtank3(500); Testtank3 = Testtank + 50; cout << "Testtank3 ist " << Testtank3.GetMax() + Testtank3.GetExtra() << "l groß und fasst momentan " << Testtank3.GetCurr() << "l." << endl; Testtank3 -= 25.75; Testtank3 += 33.33; cout << "Testtank3 ist " << Testtank3.GetMax() + Testtank3.GetExtra() << "l groß und fasst momentan " << Testtank3.GetCurr() << "l." << endl; return 0; }Fehlermeldung:
C:\...\main.cpp|16|undefined reference to `operator+(EnergySource const&, double)'|
-
Mein Vorschlag: EnergySource.cpp Zeile 40
const EnergySource& operator+(const EnergySource& Left, double Right)statt
const EnergySource& operator+(EnergySource Left, double Right)Habs nicht ausprobiert, aber bin guter Dinge, dass es klappt.
-
Klar, danke, in der Headerdatei hab ichs so und in in der cpp nicht. Danke für die Hilfe, läuft.