【问题标题】:a problem about using const char* to pass parameter关于使用 const char* 传递参数的问题
【发布时间】:2011-04-17 08:11:49
【问题描述】:

我首先将 3 值存储到一对映射中,如下所示:

void AddMenuAtlasTexture( int tag, const char* filename, const char* textureName )
{
 map<const char*, const char*> _item;
 _item.insert(pair<const char*, const char*>(filename, textureName));

 m_texturesToLoad.insert(pair<int, map<const char*, const char*> >(tag, _item));
};

然后我将值传递给另一个函数,如下所示:

map<const char*, const char*>::iterator _content;
int _tag = (*m_texturesToLoadIterator).first;
_content = (*m_texturesToLoadIterator).second.begin();
AtlasManagerSingleton->AddAtlas((*_content).first, (*_content).second, _tag);

“textureName”是这样的绝对路径:“/Users/eddy/Library/Application Support/iPhone Simulator/User/Applications/5FDE0091-2E93-42FE-BB62-05A16429551D/Ranch.app/../文件/shop_tex.png"

我的问题是第一个函数可以正确获取“textureName”,但第二个函数“AddAtlas”无法获取路径,“(*_content).second”为NULL。

“AddAtlas”原型是:

void AtlasManager :: AddAtlas( const char *a_configFile, const char *a_spriteName, int a_nKey ) 

我在 iPhone 开发中使用 XCode 进行开发。

【问题讨论】:

  • 显示你用来调用AddMenuAtlasTexture的所有代码。
  • 提示:您可以使用 -> 运算符代替 (*pointer).stuff。你可以做指针->东西。
  • 尝试使用 m_texturesToLoadIterator->second 代替 (*m_texturesToLoadIterator).second 我认为这可能是运算符重载问题
  • 谢谢你的回答,但是我改成这个“_content = m_texturesToLoadIterator->second.begin();”后,还是取不到字符串。
  • 为什么你不使用 std::string?

标签: iphone char constants objective-c++


【解决方案1】:
  • 使用make_pair 而不是pair&lt;int, map&lt;const char*, const char*&gt; &gt;
  • 使用-&gt; 而不是*.
  • 纹理加载器是一个贴图int -&gt; const char* -&gt; const char*。我没有看到你在哪里使用了第二个索引。

大概是这样的:

AtlasManagerSingleton->AddAtlas((*_content).first, (*_content).second, _tag)

应该是:

AtlasManagerSingleton->AddAtlas(_content->first, _content->second.begin()->second, _tag)

【讨论】:

    猜你喜欢
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多