I'm keep getting a warning when trying to run the code below: "warning: reference to stack memory associated with local variable 'batteryPack' returned [-Wreturn-stack-address]". How can I fix this to make it run correctly?
#include <iostream>
using namespace std;
class Battery {
public:
Battery() {} // default constructor
Battery(double voltage, double maxStore, double curEnergy);
Battery BatteryA(double voltage, double maxStore, double curEnergy);
double powerDevice(double curEnergy, double voltage);
double maxTime(double curEnergy, double time, double voltage);
double reCharge(double maxStore, double curEnergy);
double testBattery(Battery);
private:
double voltage;
double maxStore;
double curEnergy;
friend Battery& operator+ (Battery&, Battery&);
friend istream& operator >> (istream&, Battery&);
friend ostream& operator << (ostream&, const Battery&);
};
Battery::Battery(double battVoltage, double battMaxStore, double battCurEnergy) {
voltage = battVoltage;
maxStore = battMaxStore;
curEnergy = battCurEnergy;
}
Battery batteryA(double voltage, double maxStore, double curEnergy) {
Battery batteryA;
voltage = 12.0;
maxStore = 5.00e+006;
curEnergy = 5.00e+006;
return(batteryA);
}
istream & operator >> (istream &is, Battery &batteryB){
is >> batteryB.voltage >> batteryB.maxStore >> batteryB.curEnergy;
return is;
}
ostream& operator<<(ostream&os, const Battery&batteryA) {
os << batteryA.voltage << fixed << "-V," << batteryA.maxStore << scientific << "J," << batteryA.curEnergy << scientific << " J" << endl;
return os;
}
double Battery::maxTime(double curEnergy, double time, double voltage) {
time = 52083 - (curEnergy/(voltage*8));
return (time);
}
double Battery::powerDevice(double curEnergy, double voltage) {
double w;
w = 900*voltage*4;
if (w < curEnergy) {
curEnergy = curEnergy - w;
}
return (curEnergy);
}
double Battery::reCharge(double maxStore, double curEnergy) {
curEnergy = maxStore;
return (maxStore);
}
double Battery::testBattery(Battery) {
double time = 52083;
Battery BatteryA;
cout << "How long the battery's remaining energy can charge (in seconds): " << endl;
cout << BatteryA.maxTime(curEnergy, time, voltage) << endl;
cout << "How long the battery's remaining energy can charge after recharging (in seconds): " << endl;
cout << time << endl;
return (0);
}
Battery& operator+ (Battery &A, Battery &B) {
Battery batteryPack;
batteryPack.voltage = A.voltage + B.voltage;
batteryPack.maxStore = A.maxStore + B.maxStore;
batteryPack.curEnergy = A.curEnergy + B.curEnergy;
return batteryPack;
}
The issue seems to revolve around the final lines of the above section. The remaining code is shown below.
int main() {
Battery batteryA(12, 15, 13);
Battery batteryB(13, 16, 14);
Battery batteryPack;
batteryPack = batteryA + batteryB;
cout << "Initial battery as constructed: " << batteryA << endl << endl;
cout << batteryA.testBattery(batteryA) << endl << endl << endl;
cout << "Enter battery to place in the battery pack: (";
cin >> batteryB;
cout << ")" << endl << endl;
cout << batteryPack.testBattery(batteryPack);
system("pause");
return 0;
}