我正在尝试使用 NDK 和 Eclipse CDT 将以下简单的 C++ 代码编译为原生 Android 代码:
#include <vector>
using namespace std;
class Pt {
public:
Pt(int _x, int _y);
int x;
int y;
};
Pt::Pt(int _x, int _y){
x = _x;
y = _y;
}
void test(){
std::vector<Pt> pts;
pts.push_back(Pt(2,3));
int i = pts[0].x; //error here
}
我可以从命令行使用 ndk-build.cmd
毫无问题地编译代码,我什至可以从 Eclipse 中编译它。问题是在最后一行(此处有//error 注释),Eclipse 编辑器报告以下错误:
Field 'x' could not be resolved
可能的解决方案是:
- 像这样写
pts[0].x
:int i = ((Pt)pts[0]).x;
- 使用 var:
Pt apt = pts[0]; int i = apt.x;
(令人惊讶的是,这有效)
我花了将近 2 天的时间尝试使用自定义路径设置 Eclipse 以包含文件、使用索引器、升级到最新的 NDK 以及我能想到的一切。问题依然存在。这个问题显然出现在每个采用参数化类型的类中(不仅是 vector )。 尽管 Eclipse 确实编译了代码,但报告此错误的事实导致 Android 项目被标记为“有错误”,从而无法将其作为一个整体运行。
非常感谢任何帮助, 谢谢
最佳答案
可能是this可以帮助你,你必须在项目属性中包含正确的 STL header 。
关于android - 使用 Android NDK 时,Eclipse 编辑器将无法识别 C++ 模板类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22330773/