2012-10-26 55 views
0

我想創建一個仿函數像這樣:C++:如何創建仿

struct STFRandomTreeFunction 
{ 
    typedef double (*function_ptr)(const TrainingDataPoint& 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_ptr, std::string function_id){ 
     image_cache = &cache; 
     this->function = function; 
     this->function_id = function_id; 
    } 

    std::string get_function_id(){ 
     return function_id; 
    } 

    double operator()(const TrainingDataPoint& data_point){ 
     return function(data_point, *image_cache); 
    } 
}; 

,我試圖運行下面的代碼:

double something(const TrainingDataPoint& a, unordered_map<string, Mat>& cache){ 
    return 5; 
} 


int main(int argc, char* argv[]) { 

    unordered_map<string, Mat> cache; 

    STFRandomTreeFunctor functor = STFRandomTreeFunctor(cache, something, "id"); 

    TrainingDataPoint d = TrainingDataPoint(1,2,"", ImagePoint(1,2), ImagePoint(1,2), ""); 

    double value = functor(d); 

    cout << "Value is " << value << endl; 
} 

但是,我沒有得到任何輸出。它看起來好像有一些異常拋出,但是eclipse沒有顯示出來,但只是顯示程序已經終止。

任何想法?

回答

4

STFRandomTreeFunctor

this->function = function; 

構造這是自賦值。我假設你的意思是這樣做的:

this->function = function_ptr; 
+0

艾格,花了**遠**這個太長了:非常感謝 :)。接受5分鐘即將到來 – Aly