[英]C++ std::partition compile error
I'm trying to partition an array in order to do a Quick Sort. 我正在尝试对数组进行分区以进行快速排序。 I have not coded it fully as I'm getting a compile error in the partition part. 由于在分区部分出现编译错误,因此我没有对其进行完整的编码。 I'm not sure what to make of the error listing. 我不确定该如何处理错误列表。
Here is the header for a weighted point struct 这是加权结构的标头
#ifndef WEIGHTED_POINT_H
#define WEIGHTED_POINT_H
struct WeightedPoint {
_int64 pointWeight;
int xCoordinate;
int ycoordinate;
};
bool compareWeightedPoints(WeightedPoint p1, WeightedPoint p2);
#endif
Here is the CPP for it 这是它的CPP
#include "WeightedPoint.hpp"
bool compareWeightedPoints(WeightedPoint p1, WeightedPoint p2) {
return p1.pointWeight < p2.pointWeight;
}
Here is the main method 这是主要方法
#include <algorithm>
#include <vector>
#include "WeightedPoint.hpp"
const int VECTOR_SIZE = 100;
WeightedPoint* partitionWeightedPoints(WeightedPoint* first, WeightedPoint* last) {
WeightedPoint* key = first;
WeightedPoint* middle = std::partition(first + 1, last, [=](const WeightedPoint *data)->bool {return data->pointWeight < key->pointWeight; }) - 1;
}
void fillWeightedPointsVector(std::vector<WeightedPoint> inputData) {
int counter = 0;
while (counter++ < VECTOR_SIZE) {
inputData.push_back({ std::rand(), std::rand(), std::rand() });
}
}
int main() {
std::vector<WeightedPoint> inputData;
fillWeightedPointsVector(inputData);
WeightedPoint* arrayToSort = inputData.data();
int numberOfElements = inputData.size();
partitionWeightedPoints(&arrayToSort[0], &arrayToSort[numberOfElements - 1]);
}
Here is the compilation error 这是编译错误
1>------ Build started: Project: TestPartition, Configuration: Debug Win32 ------
1> WeightedPoint.cpp
1> MainSource.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm(2141): error C2664: 'bool partitionWeightedPoints::<lambda_a5c326daa6382dcb3f8cf628cbfa8999>::operator ()(const WeightedPoint *) const' : cannot convert argument 1 from 'WeightedPoint' to 'const WeightedPoint *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm(2164) : see reference to function template instantiation '_BidIt std::_Partition<_Iter,_Pr>(_BidIt,_BidIt,_Pr,std::bidirectional_iterator_tag)' being compiled
1> with
1> [
1> _BidIt=WeightedPoint *
1> , _Iter=WeightedPoint *
1> , _Pr=partitionWeightedPoints::<lambda_a5c326daa6382dcb3f8cf628cbfa8999>
1> ]
1> c:\users\gopalmenon\documents\visual studio 2013\projects\testpartition\testpartition\mainsource.cpp(11) : see reference to function template instantiation '_FwdIt std::partition<WeightedPoint*,partitionWeightedPoints::<lambda_a5c326daa6382dcb3f8cf628cbfa8999>>(_FwdIt,_FwdIt,_Pr)' being compiled
1> with
1> [
1> _FwdIt=WeightedPoint *
1> , _Pr=partitionWeightedPoints::<lambda_a5c326daa6382dcb3f8cf628cbfa8999>
1> ]
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm(2146): error C2664: 'bool partitionWeightedPoints::<lambda_a5c326daa6382dcb3f8cf628cbfa8999>::operator ()(const WeightedPoint *) const' : cannot convert argument 1 from 'WeightedPoint' to 'const WeightedPoint *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'm not sure what to make of it. 我不确定该怎么做。 I'm not too good at C++. 我不太擅长C ++。 Any help would be much appreciated. 任何帮助将非常感激。
std::partition
expects the predicate to take a const reference (ie const WeightedPoint&
in this case), but your predicate signature takes a const pointer. std::partition
期望谓词采用const引用(在这种情况下,即const WeightedPoint&
),但是您的谓词签名采用const指针。 This is where your current compile error comes from. 这是您当前的编译错误出处。 To remove that, for example change 要删除它,例如更改
[=](const WeightedPoint *data)->bool {return data->pointWeight < key->pointWeight; }
to 至
[=](const WeightedPoint& data)->bool {return data.pointWeight < key->pointWeight; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.