2015-04-06 48 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:因爲你的問題是「對一系列有價格的汽車進行排序」。因此,我假設你想根據價格對汽車進行分類。不是嗎? –