问题描述
也许我遗漏了一些东西,但由于某种原因,重载的 operator=
可以成功地与 Fuel
变量一起使用,但不能与 Capacity
一起使用,即使它们具有完全相同的属性。
class CGasstation
{
private:
int Capacity[4];
int Fuel[4];
int PistolSize;
public:
CGasstation(int StartingFuel = 100,int StartingCapacity = 200,int StartingPistolSize = 10);
virtual ~CGasstation();
void SetCapacity(int NewCapacity,int StorageNum);
void SetPistolSize(int);
void AddFuel(int,int StorageNum);
float FuelUpCar(float,int StorageNum);
void FixFuel();
void FixCapacity();
void GetInfo(int StorageNum);
/*__________________________________________________________________________________________________________________________________________________________________*/
CGasstation& operator= (const CGasstation & NewSize);
/*__________________________________________________________________________________________________________________________________________________________________*/
CGasstation& operator= (const CGasstation & NewSize);
#include "Gasstation.h"
#include <iostream>
using namespace std;
CGasstation::CGasstation(int StartingFuel,int StartingCapacity,int StartingPistolSize)
{
for (int i = 0; i < 4; i++)
{
Capacity[i] = StartingCapacity;
Fuel[i] = StartingFuel;
}
PistolSize = StartingPistolSize;
}
void CGasstation::FixFuel()
{
for (int i = 0; i < 4; i++)
{
if (Fuel[i] > Capacity[i])
{
Fuel[i] = Capacity[i];
}
}
}
void CGasstation::FixCapacity()
{
for (int i = 0; i < 4; i++)
{
if (Capacity[i] < 50)
{
Capacity[i] = 50;
}
}
}
CGasstation::~CGasstation()
{
}
void CGasstation::GetInfo(int StorageNum)
{
cout << "Capacity: " << Capacity[StorageNum] << endl << "Fuel: " << Fuel[StorageNum] << endl << "Pistol Size: " << PistolSize << endl << "________________________________________________________________________________________________________________________" << endl;
}
void CGasstation::SetCapacity(int NewCapacity,int StorageNum)
{
Capacity[StorageNum] = NewCapacity;
if (Capacity[StorageNum] < 50)
{
Capacity[StorageNum] = 50;
}
}
void CGasstation::SetPistolSize(int NewPistolSize)
{
PistolSize = NewPistolSize;
if (PistolSize < 10)
{
PistolSize = 10;
}
if (PistolSize > 300)
{
PistolSize = 300;
}
}
void CGasstation::AddFuel(int AdditionalFuel,int StorageNum)
{
if (AdditionalFuel < 0)
{
AdditionalFuel = 0;
}
Fuel[StorageNum] = Fuel[StorageNum] + AdditionalFuel;
}
float CGasstation::FuelUpCar(float SoldFuel,int StorageNum)
{
if (SoldFuel < 0)
{
SoldFuel = 0;
}
if (SoldFuel > Fuel[StorageNum])
{
SoldFuel = Fuel[StorageNum];
}
Fuel[StorageNum] -= SoldFuel;
return (SoldFuel / PistolSize);
}
/*__________________________________________________________________________________________________________________________________________________________________*/
CGasstation& CGasstation::operator= (const CGasstation& NewSize)
{
for (int i = 0; i < 4; i++)
{
Capacity[i] = NewSize.Capacity[i];
Fuel[i] = NewSize.Fuel[i];
}
return *this;
}
#include <iostream>
#include "Gasstation.h"
using namespace std;
int main()
{
CGasstation Gasstation;
int choise,StorageNum,a,b;
while (true)
{
cout << "Menu:" << endl << "1-Get information" << endl << "2-Change capacity" << endl << "3-Change pistol size" << endl << "4-Add fuel to the storage" << endl << "5-Fuel up a car" << endl << "6-Exit" << endl << "7-Set new capacity and fuel amount for all storages" << endl << "8-increase cacity and fuel amount for all storages" << endl << endl;
cin >> choise;
cout << "________________________________________________________________________________________________________________________" << endl;
if (choise != 1 && choise != 2 && choise != 3 && choise != 4 && choise != 5 && choise != 6 && choise != 7 && choise != 8)
{
cout << "Pomilka";
break;
}
if (choise == 1)
{
cout << "Which storage would you like to check?(1-4)" << endl;
cin >> StorageNum;
cout << endl;
if (StorageNum != 1 && StorageNum != 2 && StorageNum != 3 && StorageNum != 4)
{
cout << "Pomilka";
break;
}
else
{
Gasstation.GetInfo(StorageNum - 1);
}
}
if (choise == 2)
{
cout << "Which storage would you like to change in size?(1-4)" << endl;
cin >> StorageNum;
cout << endl;
if (StorageNum != 1 && StorageNum != 2 && StorageNum != 3 && StorageNum != 4)
{
cout << "Pomilka";
break;
}
else
{
cout << "Insert new size value (cannot be less than 50)" << endl;
cin >> a;
cout << endl << "________________________________________________________________________________________________________________________" << endl;
Gasstation.SetCapacity(a,StorageNum - 1);
Gasstation.FixFuel();
}
}
if (choise == 3)
{
cout << "Insert new pistol size value (10<your size<300)" << endl;
cin >> a;
cout << endl << "________________________________________________________________________________________________________________________" << endl;
Gasstation.SetPistolSize(a);
}
if (choise == 4)
{
cout << "Which storage would you like to fill up?(1-4)" << endl;
cin >> StorageNum;
cout << endl;
if (StorageNum != 1 && StorageNum != 2 && StorageNum != 3 && StorageNum != 4)
{
cout << "Pomilka";
break;
}
else
{
cout << "How much fuel would you like to add?" << endl;
cin >> a;
cout << endl << "________________________________________________________________________________________________________________________" << endl;
Gasstation.AddFuel(a,StorageNum - 1);
Gasstation.FixFuel();
}
}
if (choise == 5)
{
cout << "Which storage would you like to use for filling?(1-4)" << endl;
cin >> StorageNum;
cout << endl;
if (StorageNum != 1 && StorageNum != 2 && StorageNum != 3 && StorageNum != 4)
{
cout << "Pomilka";
break;
}
else
{
cout << "How much would you like to use up?" << endl;
cin >> a;
cout << endl;
cout << "Time to fill up a car: " << Gasstation.FuelUpCar(a,StorageNum - 1) << " minutes" << endl << "________________________________________________________________________________________________________________________" << endl;
}
}
if (choise == 6)
{
break;
if (choise == 7)
{
cout << "What capacity and fuel amount would you like to set?" << endl << endl;
cin >> b;
Gasstation = b;
Gasstation.FixCapacity();
Gasstation.FixFuel();
cout << "________________________________________________________________________________________________________________________" << endl;
}
}
return 0;
}
把它改成显示整个程序,因为这是一个大学项目,需要我使用以前的项目,所以之前所做的一切都必须保持原样,我唯一可以改变的部分就是重载。是的,我必须超载它,我不能说没有它就很好。
结果截图如下:
Capacity
的起始金额为 200,Fuel
的起始金额为 100。在屏幕截图中,Fuel
的更改与程序预期的一样,只是 FixFuel
开始确保它不超过限制,但 Capacity
根本没有改变,我不知道为什么。
我不知道出了什么问题,这是我在这个网站上的第一个问题,所以我希望你们中的一些人可以帮助我。
这是同一问题的一个非常简短的版本:
class Testing
{
private:
int a[4];
int b[4];
public:
Testing(int startingA = 100,int startingB = 200);
void GetInfo(int Number);
/*__________________________________________________*/
Testing& operator= (const Testing& NewNum);
};
#include "Testing.h"
#include <iostream>
using namespace std;
Testing::Testing(int startingA,int startingB)
{
for (int i = 0; i < 4; i++)
{
a[i] = startingA;
b[i] = startingB;
}
}
void Testing::GetInfo(int Number)
{
cout << "a: " << a[Number] << endl << "b: " << b[Number] << endl << endl;
}
Testing& Testing::operator= (const Testing& NewNum)
{
for (int i = 0; i < 4; i++)
{
a[i] = NewNum.a[i];
b[i] = NewNum.b[i];
}
return *this;
}
#include <iostream>
#include "Testing.h"
using namespace std;
int main()
{
Testing Test;
int choise,Number,NewNum;
while (true)
{
cout << "Menu:" << endl << "1-Get info" << endl << "2-Change info" << endl << "3-exit" << endl << endl;
cin >> choise;
if (choise != 1 && choise != 2 && choise != 3)
{
cout << "error";
break;
}
if (choise == 1)
{
cout << endl << "Which number would you like to check?(1-4)" << endl << endl;
cin >> Number;
cout << endl;
if (Number != 1 && Number != 2 && Number != 3 && Number != 4)
{
cout << "error";
break;
}
else
{
Test.GetInfo(Number - 1);
}
}
if (choise == 2)
{
cout << endl << "Imput new number" << endl << endl;
cin >> NewNum;
Test = NewNum;
cout << endl;
}
if (choise == 3)
{
break;
}
}
return 0;
}
问题是,为什么值 a
会改变,而值 b
不会?我该如何解决?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)