我似乎在創建定義爲主題的結構時遇到了一些問題。C++,stl函數向量指針映射
我的目標是創建一種事件處理程序(不管它是好的還是壞的程序設計,或者它不是多線程:目前只是爲了實踐)。
然後我的想法是創建一個指向函數的指針矢量,並將該矢量放置在一個映射中,其中的鍵是一個字符串。
我必須做一些概念性錯誤的,因爲我得到一些奇怪的錯誤: 我的代碼如下(誤差在後):
.h file
//ptr to function
typedef int (*pt2Function)(void*);
typedef std::vector<pt2Function> fPtrVector;
class eventDispatcher
{
public:
//stuff
void addListener(std::string,pt2Function);
protected:
//stuff
std::map<std::string,fPtrVector> _listeners;
};
,這裏是一個cpp:
.cpp file
void eventDispatcher::addListener(std::string eventName ,pt2Function function)
{
std::map<std::string,fPtrVector>::iterator it;
it=this->_listeners.find(eventName);
if(it != this->_listeners.end())
{
//do something
}
else
{
std::vector<pt2Function> tmp;
tmp.insert(function); // here occurs error 1
this->_listeners.insert(eventName,tmp); // here occurs error 2
std::cout<<"cnt: "<< this->_listeners.count();
}
}
我得到的錯誤是:
1) no matching function for call to 'std::vector <int (*)(void*), std::allocator<int (*)(void*)> >::insert(int (*&)(void*))'
2) no matching function for call to 'std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<int (*)(void*), std::allocator<int (*)(void*)> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<int (*)(void*), std::allocator<int (*)(void*)> > > > >::insert(std::string&, std::vector<int (*)(void*), std::allocator<int (*)(void*)> >&)'
我覺得很蠢......謝謝大家的回答 – Stormsson 2012-08-07 09:23:33