C :Tusing Boost Unordered_map错误:无匹配函数调用
C++: tusing boost unordered_map error:no matching function call
我有以下代码:
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");
}
- Rcpp并行无匹配函数,用于调用"转换"
- 运算符重载错误无匹配运算符<<
- 为什么通过带有文字编号的引用调用会出现"无匹配函数"错误?
- C++:提升 ptree 删除子项:无匹配功能
- wxWidgets 运行时错误(不匹配版本)
- 错误:没有匹配的成员函数来调用"push_back"
- 无匹配函数呼叫getline()
- 错误:呼叫构造器的匹配函数无匹配功能
- C 线程 - 无匹配函数供呼叫
- C 汇编错误:无匹配的构造函数以进行初始化
- 错误代码:无匹配函数供呼叫
- 错误:如果(object1 == object2),“操作器==”无匹配(操作数类型为“ const class”和“
- 为链接列表构建迭代器类(错误:无匹配的构造函数以初始化)
- 错误:“操作员^”无匹配
- std::查找"错误无匹配函数"
- C 错误无匹配函数
- C 模板错误无匹配操作员过载
- 操作员无匹配==错误,C
- C :Tusing Boost Unordered_map错误:无匹配函数调用
- C 无匹配函数用于呼叫错误(默认为参考通过)