简体   繁体   English

C ++重载函数错误

[英]C++ Overloaded Function Error

I'm working with JNI on a current project, and am getting a strange error from my C++ code during compilation. 我正在使用当前项目的JNI,并且在编译期间从我的C ++代码中得到了一个奇怪的错误。 I get an error stating: 我收到一条错误消息:

error: overloaded function with no contextual type information 错误:没有上下文类型信息的重载函数

This is coming from the "nativegetsupportedciphersuites" line in the following array, which is mapping java functions with their C++ counterparts. 这来自以下数组中的“ nativegetsupportedciphersuites”行,该行将Java函数与其C ++对应项进行映射。 I've cut out the other array members to make it easier to read. 我剪掉了其他数组成员,以使其更易于阅读。

static JNINativeMethod sSocketImplMethods[] =
{
...
    {"nativegetsupportedciphersuites", "()[Ljava/lang/String;", (void*)&Java_mypackage_SocketImpl_nativegetsupportedciphersuites},
...
};


I think it must be an error with the type declaration, but really have no clue. 我认为类型声明一定是错误的,但实际上没有任何线索。 The type declaration was generated by the javah function, so I assume it is correct. 类型声明是由javah函数生成的,因此我认为它是正确的。 The function signature of the above method is shown below: 上述方法的功能签名如下所示:

JNIEXPORT jobjectArray JNICALL Java_mypackage_nativegetsupportedciphersuites(JNIEnv* env, jobject object)


Any Thoughts? 有什么想法吗?

Chris 克里斯

The error message indicates that you method is overloaded. 错误消息表明您的方法已重载。 The compiler can't figure out which one of the overloads you want to take a pointer to, since it doesn't have any parameter information. 由于编译器没有任何参数信息,因此它无法弄清楚您要使用哪个重载指针。

It sounds like you didn't intend to overload the method. 听起来您不打算重载该方法。 Do you have a second declaration of that method anywhere? 您在任何地方都有该方法的第二个声明吗? Are you using the exact same signature in both the header and the body? 您在标题和正文中使用的签名是否完全相同?

Generally, you shouldn't cast function pointers to void* - some platforms can't fit a function pointer in a void*. 通常,您不应该将函数指针转换为void *-一些平台无法将函数指针放入void *。 The generic function pointer type is 'void (*)()', though obviously you must cast back to the correct type before calling the function to avoid stack corruption. 通用函数指针的类型为“ void(*)()”,尽管显然您必须在调用函数之前将其强制转换为正确的类型,以避免堆栈损坏。

The error suggests that there might be two different overloads for Java_mypackage_nativegetsupportedciphersuites visible (perhaps because the signature in your .cpp file doesn't exactly match that in the javah generated .h file), and hence it can't choose the one you want based on the type you are casting to (which is just void*). 该错误表明Java_mypackage_nativegetsupportedciphersuites可能存在两种不同的重载(可能是因为.cpp文件中的签名与javah生成的.h文件中的签名不完全匹配),因此它无法选择想要的基础在您要转换的类型上(只是void *)。

Have you got 'extern "C"' correctly in place in the source file? 您在源文件中正确放置了“ extern“ C”吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM