首页 > 解决方案 > 类变量无法正常工作,因为我想要重载 operator=

问题描述

也许我遗漏了一些东西,但由于某种原因,重载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;

}

更改为显示整个程序,因为这是一个大学项目,需要我使用以前的项目,所以之前制作的所有内容都必须保持原样,我唯一可以更改的部分是重载。是的,我必须超载它,我不能说没有它就可以了。

这是结果的截图:

图片

起始金额为Capacity200,起始金额为Fuel100。在截图中,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不会?我该如何解决?

标签: c++visual-c++operator-overloading

解决方案


推荐阅读