【问题标题】:C++: tusing boost unordered_map error:no matching function callC++:使用 boost unordered_map 错误:没有匹配的函数调用
【发布时间】:2012-10-26 17:47:03
【问题描述】:

我有以下代码:

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(),这是因为当我们创建空地图时,它会尝试创建一个 STFRandomTreeFunctor?如果是这样,我该如何解决这个问题?

谢谢

【问题讨论】:

    标签: c++ boost initialization unordered-map


    【解决方案1】:

    电话

    lut["a"] = x;
    

    将首先在themap 中创建一个空条目(cfr.operator[]:“效果: 如果容器尚未包含具有等效于 k 的键的元素,则插入值 std::pair&lt;key_type const, mapped_type&gt;(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 将被复制到地图中。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        把你的结构定义放在最上面,基本上它是在尝试构造unordered_map&lt;string, STFRandomTreeFunctor&gt; 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");
        }
        

        【讨论】:

        • 哦,抱歉,我只是把它们复制错了,结构实际上是在包含的头文件中定义的,我将编辑帖子以反映这一点
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多