LoveUnix » 编程开发 & Rational » 请教:关于函数匹配的问题
让LU留住您的每

一天 让LU博客留住您的每一天
2006-1-19 18:29 fc16425
请教:关于函数匹配的问题

编了一段 linux Pthreads 多线程的代码
编译未能通过,报错如下:
In member function ' void PrimeNum::Fun(int,int)'
95: no matches converting function 'taska' to type 'void*(*)(*void)'
23: candidates are: void* PrimeNum::taska(void*)
97: no matches converting function 'taska' to type 'void*(*)(*void)'
23: candidates are: void* PrimeNum::taska(void*)

知道是类型不匹配,但不知道怎么改

源代码如下:
#include <iostream.h>
#include <math.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>

class PrimeNum
{
public:
  int N;
  int M;
  void * taska(void *);     //对taska 定义不对?
  void Fun(int, int);
};


void * PrimeNum::taska(void *q)
{
  int *t =(int *)q;

  int m1,m2,k,i;
  int sum1=0;
  int sum2=0;
  int n,m,L;

  L=*t;

  if (L == 1)
    {  
      n = N;
      m = M/2;

      bool prime;
      for(m1=n;m1<=m;m1=m1+1)
        {
          prime=true;
          k = (int)sqrt((double)m1);
          for(i=2;i <= k;i++)
            if(m1%i == 0)
              {
                prime = false;
                break;
              }
          if(prime)
            {
              cout << m1 << " ";
              sum1=sum1+1;
            }
        }
      cout << endl;
      cout << "sum1=" << sum1 << endl;
    }

  if (L == 2)
    {
      n = M/2+1;
      m = M;

      bool prime;
      for(m2=n;m2<=m;m2=m2+1)
        {
          prime=true;
          k = (int)sqrt((double)m2);
          for(i=2;i <= k;i++)
            if(m2%i == 0)
              {
                prime = false;
                break;
              }
          if(prime)
            {
              cout << m2 << " ";
              sum2=sum2+1;
            }
        }
      cout << endl;
      cout << "sum2=" << sum2 << endl;
    }
}

void PrimeNum::Fun(int N,int M)
{
  pthread_t ThreadA,ThreadB;

  int t;
  void * q=&t;
  
  t=1;
  pthread_create(&ThreadA,NULL,taska,q);      //95行 taska该如何调用?
  t=2;
  pthread_create(&ThreadB,NULL,taska,q);      //97行
  
  pthread_join(ThreadA,&q);
  pthread_join(ThreadB,&q);
}

main(int argc,char *argv[])
{
  struct timeval ts1,te1;
  float timeuse;
  pthread_t ThreadA,ThreadB;
  
  PrimeNum a;

  cout << "please input a number :" << endl << "N = " ;
  cin >> a.N;
  cout << "please input a number :" << endl << "M = " ;
  cin >> a.M;
  
  gettimeofday(&ts1,NULL);
  
  a.Fun(a.N,a.M);

  cout << endl;
  gettimeofday(&te1,NULL);

  timeuse = 1000000*(te1.tv_sec - ts1.tv_sec) + te1.tv_usec - ts1.tv_usec;
  timeuse /= 1000000;
  cout << "timeuse = " << timeuse << endl;

  return 0;
}


望各位大侠指教,谢谢

2006-1-23 11:56 jxppp
In member function ' void PrimeNum::Fun(int,int)'
95: no matches converting function 'taska' to type 'void*(*)(*void)'
23: candidates are: void* PrimeNum::taska(void*)
97: no matches converting function 'taska' to type 'void*(*)(*void)'
23: candidates are: void* PrimeNum::taska(void*)

pthread的线程运行函数 != 类的内部函数。
类的内部函数隐含了一个this的,
除非是类的静态函数。

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.