【发布时间】:2019-10-30 13:51:22
【问题描述】:
我需要std::set<std::pair<int,NodeA>>。所以我需要重载NodeA的
void matriceLab::aStar(int* matX,const int x, const int y, const int endX,const int endY){
std::set<std::pair<int,NodeA>> nodi;
allocaNodi(nodi,matX,x,y,endX,endY);
}
void matriceLab::allocaNodi(std::set<std::pair<int,NodeA>>& nodi, int* matX,const int x, const int y,const int endX,const int endY){
for(int i = 0; i < x; i++){
for(int j = 0; j < y; j = j + 2){
NodeA nodo(i,j,endX,endY);
std::pair <int,NodeA> pair1(i + j * x, nodo);
nodi.insert(pair1);
}
}
}
class NodeA
{
//...
bool operator<(const NodeA& a){
if(posX < a.posX){
return true;
}else{
return false;
}
}
//...
}
C:\TDM-GCC-32\lib\gcc\mingw32\5.1.0\include\c++\bits\stl_pair.h|222|错误: 'operator
C:\Users\cristina\Desktop\università pdf\Laboratorio di Programmazione\progetti c++_SFML_openGL\SFML-2019-4-Grid\NodeA.h|24|注:候选:bool NodeA::operator>
【问题讨论】:
-
bool operator<(const NodeA& a) {->bool operator<(const NodeA& a) const {? -
[Tangent] 如果您使用
if(condition) return true; else return false;,请改用return condition;。 -
为什么你需要这一套?是用来做什么的?您需要解决的实际问题是什么?也许有更好的解决方案?
-
您用于
std::set的类型以及您插入其中的方式看起来好像您真的想要std::map或std::vector。不过,你还没有展示它是如何使用的,所以我不能确定。 -
你的
operator<需要修改对象吗?如果没有,您的operator<是否标记为const?