没有合适的构造函数可以从“cocos2d :: CCObject *”转换为“std :: basic_string

时间:2013-09-21 11:48:29

标签: cocos2d-x

我尝试从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;   

1 个答案:

答案 0 :(得分:0)

CCArray是一个只能包含CCObject类的容器。通过添加你违反的std :: string对象,但这确实不是你的错 - CCArray类应该在add上进行类型检查并在那时失败。

现在它报告错误,因为objectAtIndex返回CCObject*,但您假设它包含并返回std::string类型的对象。

解决方案:使用std::vector<std::string>代替CCArray来存储字符串。