undefined reference to 'vtable
父类定义的虚析构函数,子类没有实现函数体,就会出现这个错误
子线程传多个参数
struct PlaySoundStruct
{
int type;
char fileName[50];
};
void* fmod_play_sound_thread(void* data) {
struct PlaySoundStruct *pss = (struct PlaySoundStruct*) data;
JNIEnv *env = NULL;
if(mVm->AttachCurrentThread(&env, NULL) == 0) {
fmod_play_sound(pss->type, pss->fileName);
mVm->DetachCurrentThread();
}
return reinterpret_cast<void *>(1);
}
extern "C"
JNIEXPORT void JNICALL
Java_com_wzq_myapplication_FmodNative_PlaySound(JNIEnv *env, jobject instance, jint type, jstring fileName_) {
const char *fileName = env->GetStringUTFChars(fileName_, 0);
PlaySoundStruct *pss = new PlaySoundStruct;
pss->type = type;
stpcpy(pss->fileName, fileName);
if (mVm == NULL) {
env->GetJavaVM(&mVm);
}
pthread_t pthread1;
pthread_create(&pthread1, NULL, fmod_play_sound_thread, pss);
env->ReleaseStringUTFChars(fileName_, fileName);
}
如果有多个参数要传到子线程,就需要定义一个结构体来传参,注意,结构体初始化要使用new,否则传进子线程后,结构体就已经被释放了
子线程访问变量
class MyVedioManager{
private:
int aaa;
};
b26d4beae458b745953a6f521889d79f.png
如图:直接访问私有变量是会报错的。我们通过以下方式解决
(一)定义一个public函数,在public函数访问私有变量
class MyVedioManager{
public:
void testMethod();
private:
int aaa;
};
9fd8949424485b448c8cb0ca7205f7cc.png
(二)将函数定义成友元函数
class MyVedioManager {
friend void *testThread(void *args);
private:
int aaa;
};
11111111111.png
可以看到,私有变量可以访问了
网友评论