2012-10-26 122 views
0

我有以下代码:C++:tusing升压unordered_map错误:没有匹配的函数调用

结构STFRandomTreeFunction { 的typedef双(* function_ptr)(常量STFDataPoint &数据,助推:: 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()’

但我不知道为什么升压试图调用STFRandomTreeFunctor(),这是因为当我们创建空的映射,它试图创建一个STFRandomTreeFunctor?如果是这样,我该如何解决这个问题?

感谢

回答

1

呼叫

lut["a"] = x; 

将首先在themap(cfr。operator[]:“效果: )中创建一个空条目如果容器doe s不包含具有等于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 

但这样一来,你需要一个拷贝构造函数(或move-constructor),因为新创建pair将被复制到地图。

0

放在上面你的结构定义,基本上它试图构建unordered_map<string, STFRandomTreeFunctor> lut;但找不到定义你的结构:)

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"); 
} 
+0

噢,对不起,我只是复制他们在错误的,结构是其中包含的头文件实际上定义,我会编辑帖子以反映这一点 – Aly

1

当您在主函数中使用lut["a"]时,map应返回对STFRandomTreeFunctor类型的初始化值的引用,如您所见,它没有创建和初始化该实例的参数,因此它使用您的类的默认构造函数并你的类没有默认的构造函数。所以您应该写一个默认的构造函数类或使用:

lut.insert(std::make_pair("a", STFRandomTreeFunctor(cache, a, "a"))); 
相关问题