2012-10-30 100 views
0
Info: 
Parent Class: Vehicle 
Child Class : Car & Lorry 

我有一個載體,我試圖通過提升區域C++向量排序

排序對它進行排序(myVector.begin(),myVector.end(),compareByArea);

因此在Vehicle.h

我做

class VehicleTwoD 
{ 
private: 
//some variable 
public: 
bool compareByArea(const VehicleTwoD,const VehicleTwoD); 
} 

,並在Vehicle.cpp我這樣做

bool VehicleTwoD::compareByArea(const VehicleTwoD &a,const VehicleTwoD &b) 
{ 
return a.getArea() < b.getArea(); 
} 

錯誤是VehicleTwoD沒有命名的getArea成員。

它位於我的小孩車裏&卡車和雙區也在小孩申報。

但問題是,如果我想通過這種方式來排序,可我通過使用虛擬實現它,然後我創建的孩子同樣的方法,但超載,並通過這種方式讓我區的排序

有一個更好的方法來做到這一點。

感謝您的幫助!

Additional Info: 

我有這樣的getArea()在汽車&貨車函數,它只是一個簡單的

double Car::getArea() 
{ return area;} 

double Lorry::getArea() 
{ return area;} 

但現在我在矢量有對象的列表,它們中的每一個孩子不同班級。但都得到了getArea函數,我想排序這個向量是

sortVector.assign(vehicletwod, vehicletwod + arrayCounter); 
sort(sortVector.begin(),sortVector.end(),compareArea); 

//但排序不起作用。

我不知道在哪裏創建compareArea,它可以將objectA的getArea()對象2與返回布爾結果;

我嘗試使用這個

bool VehicleTwoD::compareByArea(const VehicleTwoD &a,const VehicleTwoD &b) 
{ 
return a.getArea() < b.getArea(); 
} 

在VehicleTwoD創建它,但問題是VehicleTwoD不具備的功能的getArea,它們位於子。我應該如何得到這個修復。謝謝..

感謝幫助:

我嘗試以下

template<typename T> bool compareByArea(const T &a, const T &b) { 
    return a.getArea() < b.getArea(); 
} 

和我宣佈它在.h文件也

public: 
template<typename T> 
bool compareByArea(const T,const T); 
virtual double getArea(); 

然後在我的主要。CPP我試圖

sortVector.assign(vehicletwod, vehicletwod + arrayCounter); 
sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea); 

它給我一個錯誤

誤差請求用於sortVector.std構件compareByArea ::矢量< _TP,_ALLOC .......這是非類類型「的VehicleTwoD *」

如果我這樣做

sortVector.assign(vehicletwod, vehicletwod + arrayCounter); 
    sort(sortVector.begin(),sortVector.end(),sortVector[0].compareArea); 

我會得到錯誤compareByArea沒有在這個範圍內聲明。

我該怎麼辦?我在vehicleTwoD中聲明瞭compareByArea,它包含在main.cpp中

VehicleTwoD是父類,我也是虛函數getArea(),所以當它調用getArea時,它將使用子getArea來代替,因爲私有變量 - 區域是在孩子和功能getArea()返回區域也是在孩子

我該怎麼辦..困惑&堅持。 謝謝!

最新又曰:

我想排序compareByArea的載體,其較小的面積將排序最高,而大1將在底部。

的問題是覆蓋getArea是汽車&貨車(子類)的功能,我創建這個compareByArea在main.cpp中

sortVector是vehicletwod

我設定值進入方式的載體拷貝我的vehicletwod是這樣..

if(vehicleType=="Car") 
{ 
vehicletwod[arrayCount] = new Car(); 
vehicletwod[arrayCount].setDimension(); 
//set area 
vehicletwod[arrayCount].setArea(); 
cout << "Done setting the data"; 
} 

我如何實現由區域升序排序我的。

我在main創建了這個函數。CPP

template<typename T> bool compareByArea(const T &a, const T &b) { 
    return a.getArea() < b.getArea(); 
} 

那麼我這樣做

sortVector.assign(vehicletwod, vehicletwod + arrayCounter); 
sort(sortVector.begin(),sortVector.end(),compareByArea); 

編譯錯誤: 編譯錯誤:

no matching function for call to 'sort(std::vector<VehicleTwoD*>::iterator, std::vector<VehicleTwoD*>::iterator, <unresolved overloaded function type>)' 

note: template<class _RAIter> void std::sort (_RAIter, _RAIter) 
note: template<class _RAIter, class _Compare> void std::sort(_RAiter, _RAIter, _Compare) 

感謝

我有一個新的編譯錯誤

error: passing 'const VehicleTwoD' as 'this' argument of 'virtual double VehicleTwoD::getArea()' discards qualifers. 
error: passing 'const VehicleTwoD' as 'this' argument of 'virtual double VehicleTwoD::getArea()' discards qualifers. 

關於我的getArea是這個

//在父這樣

double VehicleTwoD::getArea() 
{ 
double area; 
area=0.00; 
return area; 
} 

車 - 兒童是

double Car::getArea() 
    { 
    return area; 
    } 
+3

一件事,你的函數簽名不匹配。另外,它實際上是否包含'getArea'? – chris

+0

錯誤消息是正確的:'VehicleTwoD'沒有名爲'getArea'的成員函數。通過查看定義可以看到。其他一些班級具有這種成員職能的事實並不相關。所以編寫成員函數。 –

+1

@ user1595932不,您必須在'VehicleTwoD'之外定義'compareByArea()'或將其設爲'static'。 –

回答

2

做一個模板函數:

template<typename T> bool compareByArea(const T *a, const T *b) { 
    return a->getArea() < b->getArea(); 
} 

那麼你可以通過它來進行排序:

sort(sortVector.begin(), sortVector.end(), compareByArea<VehicleTwoD>); 

更新:

Vehicle.h:

class VehicleTwoD { 
... 
public: 
    virtual double getArea() const; 
}; 

template<typename T> bool compareByArea(const T *a, const T *b) { 
    return a->getArea() < b->getArea(); 
} 

main.cpp中:

#include "Vehicle.h" 
#include <vector> 
... 
std::vector<VehicleTwoD*> sortVector; 
// fill sortVector 
sort(sortVector.begin(), sortVector.end(), compareByArea<VehicleTwoD>); 
... 
+0

你好,我需要虛擬getArea在VehicleTwoD – user1595932

+1

使它成爲一個模板並沒有什麼幫助,除非你也解決了真正的問題,那就是它不應該是一個成員(或者應該是靜態的) –

+0

不,你需要在'VehicleTwoD'或'T'處使用'getArea()'。 –

3

如果VehicleTwoD::getArea是公開的,那麼最簡單的事情do的作用是使compareByArea爲免費函數而不是VehicleTwoD的方法:

bool compareByArea(const VehicleTwoD &a,const VehicleTwoD &b) { 
    return a.getArea() < b.getArea(); 
} 

然後你就可以很容易地通過使用它進行排序:

std::sort(myVector.begin(), myVector.end(), compareByArea); 

在C++ 11,你可以使用lambda表達式:

std::sort(std::begin(myVector), std::end(MyVector), 
      [](const VehicleTwoD &a, const VehicleTwoD &b) { 
      return a.getArea() < b.getArea(); 
      }); 

如果getArea是不公開的,你仍然可以使用compareByArea作爲自由函數,但將其聲明爲該類的一個朋友。

更新:一個完整​​,工作示例:

#include <algorithm> 
#include <iostream> 
#include <vector> 

class VehicleTwoD { 
    public: 
    VehicleTwoD(double area) : m_area(area) {} 
    double getArea() const { return m_area; } 
    private: 
    double m_area; 
}; 

bool compareByArea(const VehicleTwoD &a,const VehicleTwoD &b) { 
    return a.getArea() < b.getArea(); 
} 

int main() { 
    std::vector<VehicleTwoD> vehicles; 
    vehicles.push_back(VehicleTwoD(3.0)); 
    vehicles.push_back(VehicleTwoD(1.0)); 
    vehicles.push_back(VehicleTwoD(2.0)); 
    std::sort(vehicles.begin(), vehicles.end(), compareByArea); 
    for (auto it = vehicles.begin(); it != vehicles.end(); ++it) { 
    std::cout << it->getArea() << std::endl; 
    } 
    return 0; 
} 
+0

你是什麼意思自由功能 – user1595932

+0

「自由功能」我的意思是不是一個類的成員的功能。 –

+0

我試圖在主要創建它,但我仍然無法得到它的工作。 – user1595932