0
4

3 回答 3

1

The call

lut["a"] = x;

will first create an empty entry in themap (cfr. operator[]: "Effects: If the container does not already contain an elements with a key equivalent to k, inserts the value std::pair<key_type const, mapped_type>(k, mapped_type())"). To construct mapped_type() it needs a default constructor.

If you want to avoid that, use the insert function:

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

But this way, you'll need a copy constructor (or a move-constructor), since the newly created pair will be copied into the map.

于 2012-10-26T17:59:11.470 回答
1

When you use lut["a"] in your main function, map should return a reference to an initialized value of type STFRandomTreeFunctor and as you see, it have no parameter to create and initialize that instance, so it use default constructor of your class and your class have no default constructor. so you should either, write a default constructor for your class or use:

lut.insert( std::make_pair("a", STFRandomTreeFunctor(cache, a, "a")) );
于 2012-10-26T18:06:48.837 回答
0

Put your struct definitions on top, basically it's trying to construct the unordered_map<string, STFRandomTreeFunctor> lut; but can't find the definition for your 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");
}
于 2012-10-26T17:54:33.637 回答