郁闷!往线程里传参,到底该怎么写?。。。。
时间:2010-07-19
来源:互联网
#include <sys/types.h>
#include <sys/kstat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sched.h>
#include <iostream>
#include <map>
#include <string>
#include <queue>
#include <list>
#include <signal.h>
#include <thread.h>
using namespace std;
typedef struct St
{
int id;
char name[256];
};
void *aa(void *x)
{
cout << (struct St *)x->id << endl;
cout << (struct St *)x->name << endl;
return NULL;
}
int main()
{
thread_t th;
int iRet = 0;
struct St *st;
st = new St;
st->id = 100;
strcpy(st->name, "ffff");
iRet = pthread_create(&th, NULL, aa, (void *)st);
pthread_join(th, NULL);
getchar();
return 0;
}
无法编译通过,始终报这个错误:
5.cpp:25: error: 'void*' is not a pointer-to-object type
5.cpp:26: error: 'void*' is not a pointer-to-object type
编译环境gcc、solaris。请大侠指点
#include <sys/kstat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sched.h>
#include <iostream>
#include <map>
#include <string>
#include <queue>
#include <list>
#include <signal.h>
#include <thread.h>
using namespace std;
typedef struct St
{
int id;
char name[256];
};
void *aa(void *x)
{
cout << (struct St *)x->id << endl;
cout << (struct St *)x->name << endl;
return NULL;
}
int main()
{
thread_t th;
int iRet = 0;
struct St *st;
st = new St;
st->id = 100;
strcpy(st->name, "ffff");
iRet = pthread_create(&th, NULL, aa, (void *)st);
pthread_join(th, NULL);
getchar();
return 0;
}
无法编译通过,始终报这个错误:
5.cpp:25: error: 'void*' is not a pointer-to-object type
5.cpp:26: error: 'void*' is not a pointer-to-object type
编译环境gcc、solaris。请大侠指点
作者: caicai15 发布时间: 2010-07-19
应该
#include <pthread.h> 吧
#include <pthread.h> 吧
作者: hellioncu 发布时间: 2010-07-19
能贴出编译输出不错,如果能将第 25 行标出来就更好,数得很辛苦
错误意思很清楚,表面原因是 void * 不是指向对象类型,根本原因是优先级问题,
将
cout << (struct St *)x->id << endl;
改为
cout << ((struct St *)x)->id << endl;
错误意思很清楚,表面原因是 void * 不是指向对象类型,根本原因是优先级问题,
将
cout << (struct St *)x->id << endl;
改为
cout << ((struct St *)x)->id << endl;
作者: drangon 发布时间: 2010-07-19
补基础。。
作者: peidright 发布时间: 2010-07-19
QUOTE:
能贴出编译输出不错,如果能将第 25 行标出来就更好,数得很辛苦
错误意思很清楚,表面原因是 void * 不 ...
drangon 发表于 2010-07-19 13:11
错误意思很清楚,表面原因是 void * 不 ...
drangon 发表于 2010-07-19 13:11
谢谢 drangon
作者: caicai15 发布时间: 2010-07-19
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28