C++std::分区编译错误
C++ std::partition compile error
我正在尝试对数组进行分区以进行快速排序。由于分区部分出现编译错误,我尚未对其进行完整编码。我不知道该如何看待错误列表。
这是加权点结构的标题
#ifndef WEIGHTED_POINT_H
#define WEIGHTED_POINT_H
struct WeightedPoint {
_int64 pointWeight;
int xCoordinate;
int ycoordinate;
};
bool compareWeightedPoints(WeightedPoint p1, WeightedPoint p2);
#endif
这是它的CPP
#include "WeightedPoint.hpp"
bool compareWeightedPoints(WeightedPoint p1, WeightedPoint p2) {
return p1.pointWeight < p2.pointWeight;
}
以下是的主要方法
#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]);
}
这是的编译错误
1>------ Build started: Project: TestPartition, Configuration: Debug Win32 ------
1> WeightedPoint.cpp
1> MainSource.cpp
1>c:program files (x86)microsoft visual studio 12.0vcincludealgorithm(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.0vcincludealgorithm(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:usersgopalmenondocumentsvisual studio 2013projectstestpartitiontestpartitionmainsource.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.0vcincludealgorithm(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 ==========
我不知道该怎么做。我不太擅长C++。任何帮助都将不胜感激。
std::partition
希望谓词采用常量引用(即本例中的const WeightedPoint&
),但谓词签名采用常量指针。这就是当前编译错误的来源。要删除它,例如更改
[=](const WeightedPoint *data)->bool {return data->pointWeight < key->pointWeight; }
至
[=](const WeightedPoint& data)->bool {return data.pointWeight < key->pointWeight; }
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- std::is_base_of表示ctor编译错误
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- Qt Cmake 错误编译"GuiSupportQt not found"
- Opengl 精度转换错误编译错误 E0415
- 库将ARM架构错误编译为架构X64
- RT 音频 Mac 错误 g++ 编译错误
- 错误编译Boost.log
- 错误编译QT创建者 / QT窗口小部件示例
- 错误编译MIPS32
- Visual Studio 2013 中的错误(编译和运行代码)
- 链接错误编译qt项目在visual 2010
- 无法用模板错误编译nsgmls
- 奇怪的错误.编译失败
- 如果有人调用c++中的方法,则强制错误(编译时)
- 来自autoconf测试的错误编译命令
- 时间限制超出错误C++编译
- SFML 2.3 和 CodeBlocks 错误编译