2012-10-30 99 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