需要帮助比较属于类的数组

时间:2019-06-16 20:23:50

标签: c++ arrays

我需要比较存储在类中的数组的帮助。它必须是成员函数,并包含我的教授设置的参数。我在调用该函数以及进行比较时遇到麻烦。

我包括了此功能,以展示如何比较公司名称并在相等时返回true或false。然后,我循环浏览以找到列表中具有相同品牌的打印功能的所有汽车。

bool AutoVehicleType::isMadeByCompany(string companyName){
       if (companyName.compare(getAutoBrand())==0) 
       return true;
       else return false;
}

void printAllVehiclesMadeBy(AutoVehicleType vehicles[], int 
noOfVehicles,string brand){ 
        for(int i=0; i<noOfVehicles;i++){
            if(vehicles[i].isMadeByCompany(brand)){ 
                vehicles[i].printVehicleInfo();
                cout << endl << "---------------" << endl;
            //loop that prints if vehicles made by the same brand
            }
        }
    }

这是我要制作的功能。我需要帮助比较函数以及从类中调用成员函数。我正在尝试使其类似于我所制作的书,但是由于它是一个数组并且不知道如何使用我的老师设置的参数进行比较,因此我遇到了问题。

bool AutoVehicleType::HaveIdenticalAmenities(AutoVehicleType 
otherVehicles){
if(vehicles[i].amenities==otherVehicles.amenities)
   return true;
else return false;
}

void printVehiclesWithIdenticalAmenities(AutoVehicleType 
vehicles[], int noOfVehicles){
    for (int i = 0; i < noOfVehicles; i++)
    {
        if(vehicles[i].HaveIdenticalAmenities(otherVehicles)) 
        {
            cout<<"Vehicles "<<Vehicles[i].getNumPlate()<<" 
            and "<< otherVehicles[].getNumPlate()<<" Have 
            Identical set of Amenites";
        }
    }
}

在摆弄这些功能时,出现很多未声明的标识符错误,原因是我猜我不知道如何正确调用它们。

2 个答案:

答案 0 :(得分:0)

您无法将数组与==进行比较(许多不使用数组的原因之一)。您需要手动进行比较,即检查数组大小是否相同,然后逐个比较每个元素。您还对在方法内部使用车辆数组感到有些困惑。 HaveIdenticalAmenities方法只比较一对车辆,而不是两排车辆。

bool AutoVehicleType::HaveIdenticalAmenities(AutoVehicleType otherVehicle) {
    if (numOfAmenities != otherVehicle.numOfAmenities)
        return false;
    for (int i = 0; i < numOfAmenities; ++i)
        if (amenities[i] != otherVehicle.amenities[i])
            return false;
    return true;
}

numOfAmenities不在您上面发布的代码中,但我似乎记得您在以前的帖子(现在已删除)中有此内容。

调用方法的方式也很混乱。我猜想目的是要在vehicles数组中找到所有具有相同设施的车辆?如果是这样,那就是这样

for (int i = 0; i < noOfVehicles; i++)
{
    for (int j = i + 1; j < noOfVehicles; j++)
    {
        if (vehicles[i].HaveIdenticalAmenities(vehicles[j])) 
        {
            cout<<"Vehicles "<<vehicles[i].getNumPlate()<<" and "<< 
                vehicles[j].getNumPlate()<<" Have Identical set of Amenites";
        }
    }
}

完全未经测试的代码。

答案 1 :(得分:0)

对于以下解释,我对您的课程进行了如下设置:

struct Amenities
{
    int x = 0;
    friend bool operator==(const Amenities& amenities1, const Amenities& amenities2)
    {
        // How you compare your Amenities goes here.
        return amenities1.x == amenities2.x;
    }
};

struct AutoVehicleType
{
    std::string GetAutoBrand() const
    {
        return "";
    }

    bool IsMadeByCompany(std::string companyName) const;

    void PrintVehicleInfo() const
    {
    }

    bool HaveIdenticalAmentities(AutoVehicleType otherVehicle) const;

    int GetNumPlate()
    {
        return 1;
    }

    Amenities amenities = {};

};

bool AutoVehicleType::IsMadeByCompany(std::string companyName) const
{
    return (companyName == this->GetAutoBrand());
}

bool AutoVehicleType::HaveIdenticalAmentities(AutoVehicleType otherVehicle) const
{
    return (this->amenities == otherVehicle.amenities);
}

我的第一个建议是使用std :: vector而不是数组,如果您选择这样做,则以下内容应与您的问题大致相符:

void PrintAllVehiclesMadeByBrand(std::vector<AutoVehicleType> vehicles, std::string brand)
{
    for (const auto& vehicle : vehicles)
    {
        if (vehicle.IsMadeByCompany(brand))
        {
            vehicle.PrintVehicleInfo();
        }
    }
}

void PrintVehiclesWithIdenticalAmenities(std::vector<AutoVehicleType> vehicles)
{
    for (size_t x = 0; x < vehicles.size(); x++)
    {
        for (size_t y = x + 1; y < vehicles.size(); y++)
        {
            if (vehicles.at(x).HaveIdenticalAmentities(vehicles.at(y)))
            {
                std::cout << "Vehicles " << vehicles.at(x).GetNumPlate() << " and " << vehicles.at(y).GetNumPlate << " have identical amenities." << std::endl;
            }
        }
    }
}

在您遇到问题的地方,我们仅循环比较向量的每个元素与其后的每个元素。

但是,由于这是一项家庭作业,因此我认为您可能需要使用一个数组(尽管除非在板子上的某个地方进行,否则我不建议这样做),而实现可能看起来如下:

>
template <typename T, size_t N>
void PrintAllVehiclesMadeByBrand(T(&vehicles)[N], std::string brand)
{
    for (size_t x = 0; x < N; x++)
    {
        if (vehicles[x].IsMadeByCompany(brand))
        {
            vehicles[x].PrintVehicleInfo();
        }
    }
}

template <typename T, size_t N>
void PrintVehiclesWithIdenticalAmenities(T(&vehicles)[N])
{
    for (size_t x = 0; x < N; x++)
    {
        for (size_t y = x + 1; y < N; y++)
        {
            if (vehicles[x].HaveIdenticalAmenities(vehicles[y]))
            {
                std::cout << "Vehicles " << vehicles[x].GetNumPlate() << " and " << vehicles[y].GetNumPlate() << " have identical amenities." << std::endl;
            }
        }
    }
}

其中T(&vehicles)[N]是传递数组大小的快捷方式,而不必使用单独的参数(尽管如果您使用该技术感到不舒服,则只需照常传递数组大小即可。)

在您的示例中,您有另一辆车,我想这是您正在研究的问题(从哪里获得的)。在我提供的示例中,我们正在将每个元素与其前面的每个元素进行比较。因此,如果您的数组具有以下形式的元素:

1 2 3 4 1

它将比较1到2,然后是1到3、1到4和1到1,它将在其中打印您的语句。然后,循环计数器x将递增,并将比较2到3(我们可以放心地跳过比较2到1,因为我们已经在x的先前迭代器中比较了1到2)。因此,到最后,您将把每个元素彼此进行比较。

似乎您可能遇到的另一个问题是如何比较两种自定义类型。为此,我建议进一步研究运算符重载: Operator Overloading (请记住,在我用来使操作员成为朋友而不是成为成员的存根中,这是一种习惯用法。)

编辑:由于Vehicle是对象,因此这不是完全安全的类型。因此,如果您选择使用模板化数组,请使用static _ assert来检查T是否为Vehicle或实施SFINAE的尾随返回类型是明智的。