+ -
当前位置:首页 → 问答吧 → 郁闷!往线程里传参,到底该怎么写?。。。。

郁闷!往线程里传参,到底该怎么写?。。。。

时间: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。请大侠指点

作者: caicai15   发布时间: 2010-07-19

应该

#include <pthread.h> 吧

作者: hellioncu   发布时间: 2010-07-19

能贴出编译输出不错,如果能将第 25 行标出来就更好,数得很辛苦

错误意思很清楚,表面原因是 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




    谢谢  drangon

作者: caicai15   发布时间: 2010-07-19