C :Tusing Boost Unordered_map错误:无匹配函数调用

C++: tusing boost unordered_map error:no matching function call

本文关键字:错误 无匹配 函数调用 map Unordered Tusing Boost      更新时间:2023-10-16

我有以下代码:

struct stfrandomtreefunction { typedef double(*function_ptr)(const stfdatapoint& data,boost :: unordered_map& 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, std::string function_id)
    :image_cache(&cache), function(function), function_id(function_id){}
    std::string get_function_id(){
        return function_id;
    }
    double operator()(const TrainingDataPoint& data_point){
        return function(data_point, *image_cache);
    }
};
unordered_map<string, STFRandomTreeFunctor> lut;
double a(const STFDataPoint& b, unordered_map<string, Mat>& c){
    return 5;
}
int main(int argc, char* argv[]) {
    unordered_map<string, Mat> cache;
    lut["a"] = STFRandomTreeFunctor(cache, a, "a");
}

当我尝试构建时,我会收到以下错误: boost/unordered/detail/allocate.hpp:262:1: error: no matching function for call to ‘STFRandomTreeFunctor::STFRandomTreeFunctor()’

但我不知道为什么Boost试图调用STFRandomTreeFunctor(),这是因为当我们创建空MAP时,它会尝试创建一个stfrandomtreefunctor?如果是这样,我该如何处理?

谢谢

呼叫

lut["a"] = x;

将首先在themap中创建一个空条目(cfr。operator[]:"效果:如果容器尚未包含具有相当于k的密钥的元素,请插入值 std::pair<key_type const, mapped_type>(k, mapped_type())")。要构造 mapped_type(),它需要默认的构造函数。

如果要避免这种情况,请使用insert函数:

bool inserted=lut.insert( std::make_pair("a",x) ).second;
assert(inserted); // unless it's ok to not overwrite already inserted keys

但是,这样,您需要一个复制构造函数(或一个移动组件),因为新创建的pair将被复制到地图中。

当您在主函数中使用 lut["a"]时,地图应返回到类型STFRandomTreeFunctor的初始化值的引用,如您所见,它没有参数可以创建和初始化该实例,因此使用类的默认构造函数,而您的班级没有默认的构造函数。因此,您也应该为您的班级编写默认构造函数或使用:

lut.insert( std::make_pair("a", STFRandomTreeFunctor(cache, a, "a")) );

将您的struct定义放在顶部,基本上是试图构造unordered_map<string, STFRandomTreeFunctor> lut;,但找不到struct的定义:)

struct STFRandomTreeFunction
{
    typedef double (*function_ptr)(const STFDataPoint& 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, std::string function_id)
    :image_cache(&cache), function(function), function_id(function_id){}
    std::string get_function_id(){
        return function_id;
    }
    double operator()(const TrainingDataPoint& data_point){
        return function(data_point, *image_cache);
    }
};
unordered_map<string, STFRandomTreeFunctor> lut;
double a(const STFDataPoint& b, unordered_map<string, Mat>& c){
    return 5;
}
int main(int argc, char* argv[]) {
    unordered_map<string, Mat> cache;
    lut["a"] = STFRandomTreeFunctor(cache, a, "a");
}