【问题标题】:Type 'std::string' could not be resolved.无法解析类型“std::string”。
【发布时间】:2013-09-17 08:13:26
【问题描述】:

我已经开始在 Android 中用原生 c++ 移植一些 Java 代码。我在 C++ 中使用字符串时遇到问题:

Type 'std::string' could not be resolved

有我的示例代码

#include <jni.h>
#include <lexu_me_test_native.h>
#include <string.h>
using namespace std;

JNIEXPORT jstring JNICALL Java_lexu_me_test_native_prepairToShowNative
  (JNIEnv * env, jclass javaThis, jstring str)
{
    jboolean blnIsCopy;
    jstring jstrOutput;
    char* strCOut;
    std::string ss;

    const char* strCIn = (env)->GetStringUTFChars(str , &blnIsCopy);
    // convert jstring to a char array
    // Do stuff with the char array and and store the result
    // in another char array strCOut
    (env)->ReleaseStringUTFChars(str , strCIn); // release jstring

    jstrOutput = (env)->NewStringUTF(strCOut); // convert char array to jstring
    return jstrOutput;
}

Android.mk 文件:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := native
LOCAL_SRC_FILES := native.cpp

include $(BUILD_SHARED_LIBRARY)

Application.mk 文件:

APP_STL := stlport_static

MinGW 已安装并添加到路径中。我尝试使用 android-ndk-r8e 和 android-ndk-r8-crystax-1 没有任何帮助。在 Cygwin 终端错误中:

Compile++ thumb  : native <= native.cpp
jni/native.cpp: In function '_jstring* Java_lexu_me_test_native_prepairToShowNative(JNIEnv*, jclass, jstring)':
jni/native.cpp:11:2: error: 'string' was not declared in this scope
jni/native.cpp:11:9: error: expected ';' before 'ss'

我使用的是 Win 7 64 位。谁能说说怎么解决? 谢谢。

编辑。

在 C/C++ 常规中 - 已设置路径和符号:C:\Android\android-ndk-r8e\platforms\android-14\arch-arm\usr\include

【问题讨论】:

  • #include &lt;string&gt;, &lt;string.h&gt; 是 C 库头文件。
  • 使用命名空间标准;尝试使用字符串而不是 std::string
  • @Jite 不建议这样做。 using namespace std is bad practice
  • @jrok #include 导致未解决的包含: 和 #include 也是。
  • using namespace std;的正确使用方法根本就没有。

标签: android c++ android-ndk native


【解决方案1】:

如果其他答案不起作用,请尝试以下步骤:

  1. 如果您有using namespace std;,请使用string 而不是std::string
  2. 如果#include &lt;string&gt; 不起作用并且您使用的是Linux,请尝试#include &lt;unistd.h&gt;。如果您使用其他操作系统,请使用#include &lt;cstdlib&gt;

【讨论】:

  • 嗨,我已将 std::string 更改为 string,并添加了 #include 。但是 Eclipse 显示错误“无法解析类型'字符串'”尽管在 cygwin 中编译正常。
  • 那么eclipse有问题但是我建议你尝试#include &lt;cstdlib&gt; for windows或#include &lt;unistd.h&gt; for linux而不是#include &lt;string&gt;
  • #include Eclipse 接受了它,但是警告没有在 cygwing 中隐藏和编译显示错误“'string' is not declared in this scope”
  • 现在我确定您的计算机没有字符串标题或您的 ide(Eclipse) 已损坏。
  • 如果您是 Windows 用户,我推荐 Visual Studio,因为它非常易于使用且免费。去Microsoft's website查看吧
【解决方案2】:

检查您的 Android.mkApplication.mk 文件。确保您选择了一个 STL 库并包含在 Application.mk 文件中:

Application.mk: APP_STL := gnustl_static

(gnustl_static) 可以换成你想要的STL版本,STLPort还是可以选择的

http://www.kandroid.org/ndk/docs/CPLUSPLUS-SUPPORT.html

【讨论】:

  • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
  • 哇,这是很久以前的事了。我会更新答案。但是,请参阅kandroid.org/ndk/docs/CPLUSPLUS-SUPPORT.html
猜你喜欢
  • 1970-01-01
  • 2012-10-18
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
  • 2013-12-14
  • 1970-01-01
  • 2017-07-19
相关资源
最近更新 更多