Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"operator char *()" causes error: cast from ‘char*’ to ‘char’ loses precision #379

Closed
gordeevnm opened this issue Feb 11, 2020 · 2 comments
Labels

Comments

@gordeevnm
Copy link

file test.h:

class String {
public:
    String(const char *text, int length);

    operator char *();

    operator const char *();

private:
    char *data;
};

String::operator char *() {
    return data;
}

String::operator const char *() {
    return data;
}

String::String(const char *text, int length) {
    data = const_cast<char *>(text);
}

preset class Test.java:

@Properties(
        value = {
                @Platform(
                        include = {
                                "test.h",
                        }
                ),
        }
)
public class Test implements InfoMapper {
    @Override
    public void map(InfoMap infoMap) {
    }
}

Compilation of this example fails with error:

[INFO] g++ -I/path.../include/ -I/usr/lib/jvm/java-11-openjdk/include -I/usr/lib/jvm/java-11-openjdk/include/linux /path.../jniString.cpp /path.../jnijavacpp.cpp -march=x86-64 -m64 -O3 -s -Wl,-rpath,$ORIGIN/ -Wl,-z,noexecstack -Wl,-Bsymbolic -Wall -fPIC -pthread -shared -o libjniString.so -L/path.../lib/ -Wl,-rpath,/path.../lib/ 
/path.../jniString.cpp: In function ‘void Java_org_bytedeco_baumer_String_allocate__Lorg_bytedeco_baumer_String_2I(JNIEnv*, jobject, jobject, jint)’:
/path.../jniString.cpp:469:45: error: no matching function for call to ‘String::String(String*&, jint&)’
  469 |         String* rptr = new String(ptr0, arg1);
      |                                             ^
In file included from /path.../jniString.cpp:92:
/path.../include/test.h:22:1: note: candidate: ‘String::String(const char*, int)’
   22 | String::String(const char *text, int length) {
      | ^~~~~~
/path.../include/test.h:22:28: note:   no known conversion for argument 1 from ‘String*’ to ‘const char*’
   22 | String::String(const char *text, int length) {
      |                ~~~~~~~~~~~~^~~~
/path.../include/test.h:2:7: note: candidate: ‘constexpr String::String(const String&)’
    2 | class String {
      |       ^~~~~~
/path.../include/test.h:2:7: note:   candidate expects 1 argument, 2 provided
/path.../include/test.h:2:7: note: candidate: ‘constexpr String::String(String&&)’
/path.../include/test.h:2:7: note:   candidate expects 1 argument, 2 provided
/path.../jniString.cpp: In function ‘_jobject* Java_org_bytedeco_baumer_String_asBytePointer(JNIEnv*, jobject)’:
/path.../jniString.cpp:492:42: error: cast from ‘char*’ to ‘char’ loses precision [-fpermissive]
  492 |         rptr = (char)ptr->operator char*();
      |                                          ^
/path.../jniString.cpp:493:21: warning: NULL used in arithmetic [-Wpointer-arith]
  493 |         if (rptr != NULL) {
      |                     ^~~~
@saudet saudet added the bug label Feb 12, 2020
saudet added a commit that referenced this issue Feb 13, 2020
@saudet
Copy link
Member

saudet commented Feb 13, 2020

Thanks for reporting! I've fixed that in the latest commit.

@saudet
Copy link
Member

saudet commented Apr 15, 2020

Fix included in just released version 1.5.3. Thanks again for reporting!

@saudet saudet closed this as completed Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants