gpt4 book ai didi

C++排序数组是指向类的指针

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:42 24 4
gpt4 key购买 nike

我正在尝试对包含价格的汽车数组进行排序,我似乎在对指向另一个类的数组进行排序时遇到了问题。当我尝试更改数组的顺序时出现“错误 C2106:‘=’:左操作数必须是左值”。

我附上了下面的代码。

我的排序功能。

void CarPool::Sort()
{
const int MAXVAL = 9;
int coun = countCars;
double temp;
bool swappedFlag = true;

while (swappedFlag)
{
swappedFlag = false;
for (int i = 0; i < countCars - 1; i++)
{
ptrToPool[i].getPrice();
if (ptrToPool[i].getPrice()> ptrToPool[i + 1].getPrice())
{
temp = ptrToPool[i].getPrice();
ptrToPool[i].getPrice() = ptrToPool[i + 1].getPrice(); //ERROR C2106
ptrToPool[i + 1].getPrice() = temp; //ERROR C2106
swappedFlag = true;
}
}
}
}

汽车.cpp

#pragma once
#include "car.h" // put the related header at the TOP of the list of includes
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

Car::Car(string mName, string reg, double eng, double pri)
{
// store the parameter values for this object private data
ModelName = mName;
Registration = reg;
EngineSize = eng;
Price = pri;
}

Car::Car()
{
// set up a value that shows the data not properly loaded
ModelName = "Unspecified";
}

void Car::Load(ifstream& carFile)
{
carFile>>ModelName>>Registration>>EngineSize>>Price;

}


void Car::Display()
{
cout<<setfill(' ')<<setw(10)<<ModelName<<setfill(' ')<<setw(10)<<Registration;
cout<<setfill(' ')<<setw(10)<<EngineSize<<setfill(' ')<<setw(10)<<Price<<endl;
}

double Car::Ratio() //how much it costs per cc of engine!
{
return EngineSize/Price;
}

string Car::getRegistration()
{
return Registration;
}

double Car::getPrice()
{
return Price;
}

carpool.cpp(也是第一段代码中列出的函数)

#include "carpool.h"

#include <iostream>
#include <fstream>

using namespace std;

CarPool::CarPool()
{
countCars=0; //for now
name = "None";
}

CarPool::~CarPool()
{
if (countCars>0)
{
delete [] ptrToPool;
}
}

int CarPool::Load(string fromFilename)
{
// assumes file starts with count of cars
ifstream inFile(fromFilename);
if (!inFile)
{
return -1; //oh dear no file to read
}
inFile>>countCars; //read the following number of cars
ptrToPool = new Car[countCars];
for (int i=0; i<countCars; i++)
{
ptrToPool[i].Load(inFile);
}
return 0; //successful!
}

汽车.h

#pragma once
#include <string>
using namespace std;

class Car
{
public:
// see later for the bodies of the functions!
Car(string mName, string reg, double eng, double pri);
Car();
void Load(ifstream& carFile);
void Save(ofstream& carFile);
void Display();
string getRegistration();
double getPrice();
double Ratio(); //how much it costs per cc of engine!
void setPrice(double pri);

private:
string ModelName;
string Registration;
double EngineSize;
double Price;
};

最佳答案

getPrice() 定义为返回 double 值:

double Car::getPrice()

现在,在以下语句中,您将收到错误 C2106,因为您试图将一个数字赋值(相对于一个变量):

ptrToPool[i].getPrice() = ptrToPool[i + 1].getPrice(); //ERROR C2106
ptrToPool[i + 1].getPrice() = temp; //ERROR C2106

关于C++排序数组是指向类的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29480075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com