【问题标题】:Compiling cocos2d-x via build_native.py returns: 'to_string' was not declared in this scope通过 build_native.py 编译 cocos2d-x 返回: 'to_string' 未在此范围内声明
【发布时间】:2014-05-15 15:07:39
【问题描述】:

我正在尝试通过build_native.py 脚本为Android 构建一个cocos2d-x 3.0(稳定)项目,但是当一个类使用std::to_string(或std::stoi)函数时它会挂起。在 Xcode 下构建项目完全没有问题,只是命令行编译失败。

我已经在所有使用这些函数的类中导入<string>,但没有成功。我还像这样修改了Application.mk 文件:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -DCOCOS2D_DEBUG=0 -std=c++11 -Wno-literal-suffix -fsigned-char

添加 -std=c++11 标志以确保使用 C++11 版本编译项目。

我还有什么需要做的吗?

更多

this thread 之后,我决定加入这个:

#if CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
string to_string(int t) {
    ostringstream os;
    os << t;
    return os.str();
}
#endif

在我的标题中,因为我只是将to_string 与整数输入一起使用。这不是一个好的解决方案,但工作正常......但是当它再次找到 stoi 函数时,编译器会挂起。

【问题讨论】:

  • 您可以使用boost::lexical_cast 作为两者的替代品
  • boost 库是否与 Android arm 设备和 iOS 兼容?
  • 我不知道,我对 Android 或 iOS 都不熟悉。但是你应该可以很容易地找到它。

标签: c++ c++11 cocos2d-x cocos2d-x-3.0


【解决方案1】:

我最终使用了这段代码:

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
string to_string(int t) {
  ostringstream os;
  os << t;
  return os.str();
}

int stoi(const string myString) {
  return atoi(myString.c_str());
}
#endif

【讨论】:

  • 我使用 String::createWithFormat("%d",5)->getCString();将数字转换为字符串
【解决方案2】:

尝试使用 atoi 而不是 stoi。虽然 atoi 在错误时返回零,但它适用于命令行编译

【讨论】:

  • 这里的问题是命令行编译器找不到to_stringstoi(甚至可能是atoi)函数,但是项目可以正确构建并在Xcode下运行。跨度>
  • build_native.py 命令不会在 atoi 上为我挂起,而是在 stoi 上挂起。这个改变确实解决了我之前的问题。
  • 是的,但这并不能解决to_string 的问题。
  • 同意,但是您必须使用 shift 运算符自己编写 to_string 方法,因为您已经发布了示例代码。我发现这是唯一的选择。如果有人提供更好的选择,那就太好了。
【解决方案3】:

你可以使用 sstream 库来做 int 到 str 的转换过程,它有点长,但它可以工作:

#include <sstream>
std::stringstream myStringStream ; 
myStringStream << myInteger;
myString = myStringStream.str();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-26
    • 2013-03-05
    相关资源
    最近更新 更多