gpt4 book ai didi

C++:如何创建仿函数

转载 作者:行者123 更新时间:2023-11-27 23:20:58 24 4
gpt4 key购买 nike

<分区>

我正在尝试创建一个像这样的仿函数:

struct STFRandomTreeFunction
{
typedef double (*function_ptr)(const TrainingDataPoint& data, boost::unordered_map<std::string, cv::Mat>& preloaded_images);
};


struct STFRandomTreeFunctor
{
private:
boost::unordered_map<std::string, cv::Mat> *image_cache;
STFRandomTreeFunction::function_ptr function;
std::string function_id;

public:
STFRandomTreeFunctor(boost::unordered_map<std::string, cv::Mat>& cache, STFRandomTreeFunction::function_ptr function_ptr, std::string function_id){
image_cache = &cache;
this->function = function;
this->function_id = function_id;
}

std::string get_function_id(){
return function_id;
}

double operator()(const TrainingDataPoint& data_point){
return function(data_point, *image_cache);
}
};

并且正在尝试运行以下代码:

double something(const TrainingDataPoint& a, unordered_map<string, Mat>& cache){
return 5;
}


int main(int argc, char* argv[]) {

unordered_map<string, Mat> cache;

STFRandomTreeFunctor functor = STFRandomTreeFunctor(cache, something, "id");

TrainingDataPoint d = TrainingDataPoint(1,2,"", ImagePoint(1,2), ImagePoint(1,2), "");

double value = functor(d);

cout << "Value is " << value << endl;
}

但是,我没有得到任何输出。似乎抛出了一些异常,但 Eclipse 没有向我显示异常,只是显示程序已终止。

有什么想法吗?

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