首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >std::map<std::string,JavaCPP的std::string>转换

std::map<std::string,JavaCPP的std::string>转换
EN

Stack Overflow用户
提问于 2017-12-13 02:38:03
回答 1查看 212关注 0票数 1

我是JavaCPP的新手,现在我有麻烦了。

我的遗嘱图书馆:

代码语言:javascript
运行
复制
#include <string>
#include <map>


    class TestClass {
        public:
            TestClass() {
                property["a"]="b";
            }
            const std::map<std::string,std::string>& getMap(std::string str) { 
                if (str == "a"){
                    return property; 
                }
            }
            std::map<std::string,std::string> property;
    };

TestLibrary.java

代码语言:javascript
运行
复制
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;

@Platform(include="TestLibrary.h")
public class TestLibrary { 
    public static class TestClass extends Pointer {
        static { Loader.load(); }
        public TestClass() { allocate(); }
        private native void allocate();

        public static native @ByRef KeyValueMap getMap(String str);    
    }

@Name("std::map<std::string,std::string>") public static class 
KeyValueMap extends Pointer { 
     static { Loader.load(); } 
     public KeyValueMap(Pointer p) { super(p); } 
     public KeyValueMap()       { allocate();  } 
     private native void allocate(); 

     public native long size(); 

     @Index public native @StdString BytePointer get(@StdString 
BytePointer i); 
     public native KeyValueMap put(@StdString BytePointer i, BytePointer 
value); 
} 

    public static void main(String[] args) {
        TestClass l = new TestClass();
        KeyValueMap m = l.getMap("a");
        System.out.println(m);
        //System.out.println(m.get("a"));
    }
} 

什么时候

javac -cp javacpp.jar TestLibrary.java

java -jar javacpp.jar TestLibrary

jniTestLibrary.cpp:2238:30: error:调用不带对象参数的非静态成员函数rptr =&:TestClass::getMap(Ptr0);

代码语言:javascript
运行
复制
             ~~~~~~~~~~~~~^~~~~~

上面的代码是从NativeLibrary示例中修改的。但是如何解决编译问题呢?我能那样使用m.get("a")吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-13 04:09:38

我通过改变.

测试图书馆h:

代码语言:javascript
运行
复制
#include <string>
#include <map>


class TestClass {
    public:
        TestClass() {
            property["a"]="b";
        }
        std::map<std::string,std::string>& getMap(std::string str) { 
            if (str == "a"){
                return property; 
            }
        }
        std::map<std::string,std::string> property;
};

TestLibrary.java

代码语言:javascript
运行
复制
import org.bytedeco.javacpp.*;
import org.bytedeco.javacpp.annotation.*;

@Platform(include="TestLibrary.h")
public class TestLibrary { 
    public static class TestClass extends Pointer {
    static { Loader.load(); }
    public TestClass() { allocate(); }
    private native void allocate();

    public native @ByRef KeyValueMap getMap(String str);    
}

@Name("std::map<std::string,std::string>") 
public static class KeyValueMap extends Pointer { 
 static { Loader.load(); } 
 public KeyValueMap(Pointer p) { super(p); } 
 public KeyValueMap()       { allocate();  } 
 private native void allocate(); 

 public native long size(); 

 @Index public native @StdString BytePointer get(@StdString BytePointer i); 
 public native KeyValueMap put(@StdString BytePointer i, BytePointer value); } 

public static void main(String[] args) {
    TestClass l = new TestClass();
    KeyValueMap m = l.getMap("a");
    System.out.println(m.size());
    System.out.println(m.get(new BytePointer("a")).getString());
}} 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47784687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档