【问题标题】:std::map<std::string,std::string> conversion of JavaCPPJavaCPP 的 std::map<std::string,std::string> 转换
【发布时间】:2017-12-13 02:38:03
【问题描述】:

我是JavaCPP的新手,现在遇到了问题。

我的 TestLibrary.h:

#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

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 测试库

jniTestLibrary.cpp:2238:30:错误:调用非静态成员 没有对象参数的函数 rptr = &::TestClass::getMap(ptr0);
~~~~~~~~~~~~~^~~~~~~

上面的代码是根据 NativeLibrary 示例修改的。但是如何解决编译问题? 我可以这样使用m.get("a") 吗?

【问题讨论】:

    标签: javacpp


    【解决方案1】:

    我设法通过改变来做到这一点......

    TestLibrary.h:

    #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

    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());
    }} 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-28
      • 2015-10-22
      • 2020-03-28
      • 1970-01-01
      相关资源
      最近更新 更多