我尝试从CCDictionary allKeys()方法获取std字符串键 但是当我试图从CCArray获取密钥时,我得到了堆栈 这就是我所拥有的:
CCArray* pAllkeys = CCArray::create();
pAllkeys->addObjectsFromArray(GameSingleTone::getInstance()->getGemsDictionary()->allKeys());
pAllkeys->retain();
这里我尝试获取密钥但是我得到了编译错误:
int gemsToRemoveCount = pAllkeys ->count();
for(int i=0;i<gemsToRemoveCount;i++)
{
std::string gemKey = pAllkeys->objectAtIndex(i);
}
这是我得到的编译错误: 4
IntelliSense: no suitable constructor exists to convert from "cocos2d::CCObject *" to "std::basic_string<char, std::char_traits<char>, std::allocator<char>>"
更新
在挖掘标题后找到解决方案
CCString* name = dynamic_cast<CCString*>(pAllkeys->objectAtIndex(i));
std::string newkey = name->m_sString;
答案 0 :(得分:0)
CCArray是一个只能包含CCObject类的容器。通过添加你违反的std :: string对象,但这确实不是你的错 - CCArray类应该在add上进行类型检查并在那时失败。
现在它报告错误,因为objectAtIndex
返回CCObject*
,但您假设它包含并返回std::string
类型的对象。
解决方案:使用std::vector<std::string>
代替CCArray来存储字符串。