gpt4 book ai didi

c++ - bool 类型的参数不匹配

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:24:03 25 4
gpt4 key购买 nike

我正在尝试对数据包含我制作的对象的节点 vector 进行排序。我正在比较的是该对象的值(相关性)。我得到一个错误:

BST.h:148: error: argument of type âbool (BST<Play>::)(BST<Play>::node*, BST<Play>::node*)â does not match âbool (BST<Play>::*)(BST<Play>::node*, BST<Play>::node*)â

我从来没有见过这样的错误,它在我调用我的排序的线路上被调用。

#ifndef BST_H
#define BST_H
#include "Play.h"
#include <algorithm>
#include <vector>
#include <iostream>

template<typename T>
class BST
{
public:
struct node{
T data;
node *left;
node *right;
node *parent;

node(const T & theData, node *lt, node *rt, node *pt):
data(theData), left(lt), right(rt), parent(pt) {}
};
std::vector<node*> tempVec;
bool sorterFunc(node* x, node* y) {
return (y->data.getRelevance() < x->data.getRelevance());
}
void list(std::string offense, std::string defense, int downforplay, int yardstogo, int yardline, int minutes, node* &t, int level){
tempVec.clear();
float relevance;
if(t==NULL){
return;
}
if(level ==1){
tempCount--;
if(offense==t->data.getoTeam()){
if(downforplay==t->data.getDown()){
int yardabove = yardstogo+1;
int yardbelow = yardstogo-1;
if(yardstogo==t->data.getYard() || yardbelow==t->data.getYard() || yardabove==t->data.getYard()){
if(yardline >= t->data.getStartLoc() - percentback(yardline) && yardline <= (t->data.getStartLoc() + percentback(yardline))){
relevance= -((float)abs(minutes - t->data.getMinutes())*(float(5/3)) + (float)abs(yardstogo - t->data.getYard()) + (float)abs(yardline - t->data.getStartLoc()));
if(defense == t->data.getdTeam())
relevance += 100;
t->data.setRelevance(relevance);
tempVec.push_back(t);
}
}
}
}
}
else{
list(offense, defense, downforplay, yardstogo, yardline, minutes, t->left, level-1);
list(offense, defense, downforplay, yardstogo, yardline, minutes, t->left, level-1);
}
}
void listFunc(std::string offense, std::string defense, int downforplay, int yardstogo, int yardline, int n, int minutes){
tempCount = treeCount;
for(int i=1; tempCount>0; i++)
list(offense, defense, downforplay, yardstogo, yardline, minutes, root, i);
std::sort(tempVec.begin(), tempVec.end(), sorterFunc);
for(int i=0; i<n; i++)
std::cout << tempVec[i]->data.getwholePlay() << std::endl << tempVec[i]->data.getRelevance();
}
}

最佳答案

sorterFunc 是一个成员函数。因此,它只能在实例上工作。但是,您尝试将其用作比较 node* 的函数:

std::sort(tempVec.begin(), tempVec.end(), sorterFunc);

现在 std::sort 将在某个地方调用 sorterFunc,但这会失败,因为没有实例可以使用:

       sorterFunc( someNodePointer, someOtherNodePointer);
// ^^. missing instance

您不希望 sorterFunc 在任何实例上工作。改为 static:

static bool sorterFunc(node* x, node* y) { 
return (y->data.getRelevance() < x->data.getRelevance());
}

关于c++ - bool 类型的参数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19821507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com