我有一个static std::map<std::string, CreateGUIFunc>
在基本上持有字符串标识gui类型的类,CreateGUIFunc是对工厂函数的引用。std ::只读位置的地图分配
在我的构造函数中我有
if (m_factoryRef.size() == 0) {
m_factoryRef["image"] = &CreateGUI<Image>;
m_factoryRef["button"] = &CreateGUI<Button>;
}
...
然而,这给了我一个错误说assignment of read-only location ‘GUIManager::m_factoryRef.std::map<_Key, _Tp, _Compare, _Alloc>::operator[] [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = GUI*(), _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, GUI*()> >](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)(& std::basic_string<char, std::char_traits<char>, std::allocator<char> >(((const char*)"image"), ((const std::allocator<char>&)((const std::allocator<char>*)(& std::allocator<char>())))))))’|
我不知道为什么这是一个只读的分配。我也尝试将其更改为普通成员,以查看是否它可能是静态的,但同样的事情。
什么问题?
编辑:一些定义,以使事情多一点明确
// these are all private
typedef GUI* CreateGUIFunc();
template<class T>
GUI* GUIManager::CreateGUI(std::string &filepath, int x, int y) {
return new T(filepath, x, y);
}
static std::map<std::string, CreateGUIFunc> m_factoryRef;
附:如果有更清晰的方式来初始化静态地图请让我知道。
你可以修改地图的定义,并添加CreateGUIFunc和CreateGUI的定义(不是完整的类,只是为了让我们看看它是什么),甚至更好的可能是最小的selfcontained程序的整个代码重现问题。 – nus 2010-06-26 00:10:59
据我可以告诉你有模板和参考有点混在这里。 '&'前缀不是引用,但是如果我没有错误,则“操作符的地址”和“CreateGUI”不是对象的有效名称。 –
nus
2010-06-26 00:26:40
如何声明'm_factoryRef'? – 2010-06-26 00:27:26