2015-04-06 53 views
2

我想排序的汽车价格在他们的数组,我似乎有一个问题排序数组是指向另一个类的指针。当我尝试改变数组的顺序时,我得到“错误C2106:'=':左操作数必须是l值”。C++排序数组是指向一个类的指针

我附上了下面的代码。

我的排序功能。

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; 
      } 
     } 
    } 
} 

car.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! 
} 

car.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; 
}; 
+0

你需要交换汽车本身,而不是价值。 “ptrToPool [i] = ptrToPool [i + 1];”等。另外,我强烈建议使用更多的标准库函数。特别是,std :: sort会为你完成大部分工作。您也可以使用std :: swap进行交换。你也可以使用std :: vector而不是数组。当你使用标准库函数时,你的代码往往更简单,更快速,并且更无错误。 – Lalaland

回答

0

getPrice()在定义中返回一个dou ble:

double Car::getPrice() 

现在,在下面的语句中,您将得到ERROR C2106,因为您尝试将一个赋值指定为一个数字(vs.一个变量):

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

使用std::swap(上车的物体),你得到的错误(你需要的move分配和构造尝试定义)。

标准库为您实现它 - 现在去使用它。

PS:你得到的错误是因为你的getter函数返回一个值而不是引用(你可以给它赋值)。

如果您不想使用标准库,则可以让getter方法返回一个引用,或者您可以在=的左侧使用setter函数。

0

问题: 鉴于类的实现,您不能使用getPrice()设置价格,因为它只是一个getter而不是setter。因此,行:

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

预计会给等式左边的错误。

解决方案: 您需要一个setter。喜欢的东西:

ptrToPool[i].setPrice(someValue); 

示例实现: 尝试添加下面的方法到类:

void Car::setPrice(double pri) 
{ 
    Price=pri; 
} 

然后调用此方法来更新价格如下(与更换你的两行):

ptrToPool[i].setPrice(ptrToPool[i + 1].getPrice()); 
ptrToPool[i + 1].getPrice(temp); 

附加: 虽然这会解决当前的错误消息问题,但仍需要修改排序算法。

  1. 您想对汽车或价格进行分类吗?供参考:交换价格不会影响其他数据!
  2. 你试图实现什么样的排序算法?插入排序或选择排序?请记住他们是O(nxn)。对于大数据,您可以使用“快速排序”的库排序(即std :: sort),并且O(nxlogn)的时间复杂度要快得多。你可以参考:

http://www.cplusplus.com/reference/algorithm/sort/?kw=sort

编辑分拣:

对于价格升序排序的汽车,你可以做到以下几点:

首先,包括以下使用图书馆排序:

#include <algorithm> 

其次,根据价格将小于(<)的操作员超载添加到您的汽车级别。 (编辑:作为n0rd建议,而不是操作符重载,你可以定义更通用的方法自定义比较有一个例子,如何在上面的链接做) 你最后一类看起来像:

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); 
    bool operator < (const Car& car) const 
    { 
     return (Price < car.Price); 
    } 

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

最后在您的排序功能只要致电:

std::sort(ptrToPool, ptrToPool + size); 

因此您的最终排序功能将是以下(是的,这很短!):

void CarPool::Sort() 
{ 
    std::sort(ptrToPool, ptrToPool + countCars); 
} 

物权法请将您的排序功能替换为此功能,并且应该按照升序排列车辆价格。

希望有帮助!

+0

谢谢,非常好的解释。 – BeginnerLK

+0

我还不确定我会如何分类汽车,而不仅仅是价格。我是否必须为所有数据成员创建一个setter方法?@erol yeniaras – BeginnerLK

+0

@BeginnerLK:因为你的问题是“对一系列有价格的汽车进行排序”。因此,我假设你想根据价格对汽车进行分类。不是吗? –