为什么编译器明明没有错误却在这里抱怨?

发布于 2024-12-12 13:45:23 字数 2289 浏览 4 评论 0原文

每当我尝试编译以下程序时,我都会从编译器(g++ 4.4.3)收到此消息。任何想法,为什么?

main.cpp: In function ‘int main(int, char**)’:
main.cpp:52: error: void value not ignored as it ought to be

第 52 行的代码为 rc = pthread_create_with_stack( &thread[t], BusyWork, t );

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 4

void *stackAddr[NUM_THREADS];
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;

void *BusyWork(void *t)
{
   int i;
   long tid;
   double result=0.0;
       tid = (long)t;
   printf("Thread %ld starting...\n",tid);
   for ( i = 0; i < 1000; i++)
   {
      result = result + sin(i*tid) * tan(i*tid);
   }
   printf("Thread %ld done. Result = %e\n", tid, result);
   pthread_exit((void*) t);
}

void pthread_create_with_stack( pthread_t * pthread, void *(*start_routine) (void *), int tid )
{
    const size_t STACKSIZE = 0xC00000; //12582912
    int rc;
    size_t i;
    pid_t pid;

    stackAddr[tid] = malloc(STACKSIZE);
    pthread_attr_setstack(&attr, stackAddr[tid], STACKSIZE);

    rc = pthread_create( pthread, &attr, start_routine, (void*)0 );
}

int main (int argc, char *argv[])
{
   int rc;
   long t;
   void *status;

   /* Initialize and set thread detached attribute */
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

   for(t=0; t<NUM_THREADS; t++) 
   {
      printf("Main: creating thread %ld\n", t);
      // The following line is the line 52, where error occurs
      rc = pthread_create_with_stack( &thread[t], BusyWork, t ); 
      if (rc) 
      {
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Free attribute and wait for the other threads */
   pthread_attr_destroy(&attr);
   for(t=0; t<NUM_THREADS; t++) 
   {
      rc = pthread_join(thread[t], &status);
      if (rc) 
      {
         printf("ERROR; return code from pthread_join() is %d\n", rc);
         exit(-1);
      }
      printf("Main: completed join with thread %ld having a status"   
            "of %ld\n",t,(long)status);
    }

    printf("Main: program completed. Exiting.\n");
    pthread_exit(NULL);
}

Whenever I try to compile the following program, I get this message from the compiler (g++ 4.4.3). Any idea, why?

main.cpp: In function ‘int main(int, char**)’:
main.cpp:52: error: void value not ignored as it ought to be

Line 52 has the code rc = pthread_create_with_stack( &thread[t], BusyWork, t );

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 4

void *stackAddr[NUM_THREADS];
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;

void *BusyWork(void *t)
{
   int i;
   long tid;
   double result=0.0;
       tid = (long)t;
   printf("Thread %ld starting...\n",tid);
   for ( i = 0; i < 1000; i++)
   {
      result = result + sin(i*tid) * tan(i*tid);
   }
   printf("Thread %ld done. Result = %e\n", tid, result);
   pthread_exit((void*) t);
}

void pthread_create_with_stack( pthread_t * pthread, void *(*start_routine) (void *), int tid )
{
    const size_t STACKSIZE = 0xC00000; //12582912
    int rc;
    size_t i;
    pid_t pid;

    stackAddr[tid] = malloc(STACKSIZE);
    pthread_attr_setstack(&attr, stackAddr[tid], STACKSIZE);

    rc = pthread_create( pthread, &attr, start_routine, (void*)0 );
}

int main (int argc, char *argv[])
{
   int rc;
   long t;
   void *status;

   /* Initialize and set thread detached attribute */
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

   for(t=0; t<NUM_THREADS; t++) 
   {
      printf("Main: creating thread %ld\n", t);
      // The following line is the line 52, where error occurs
      rc = pthread_create_with_stack( &thread[t], BusyWork, t ); 
      if (rc) 
      {
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }

   /* Free attribute and wait for the other threads */
   pthread_attr_destroy(&attr);
   for(t=0; t<NUM_THREADS; t++) 
   {
      rc = pthread_join(thread[t], &status);
      if (rc) 
      {
         printf("ERROR; return code from pthread_join() is %d\n", rc);
         exit(-1);
      }
      printf("Main: completed join with thread %ld having a status"   
            "of %ld\n",t,(long)status);
    }

    printf("Main: program completed. Exiting.\n");
    pthread_exit(NULL);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

分开我的手 2024-12-19 13:45:23

pthread_create_with_stack 返回 void,但您试图将此 void“值”保存在 int 中,即一个错误。

pthread_create_with_stack returns void, yet you're trying to save this void "value" in an int, which is an error.

做个少女永远怀春 2024-12-19 13:45:23

就是这一行

rc = pthread_create_with_stack( &thread[t], BusyWork, t );

您对 pthread_create_with_stack 的定义是 void 类型。应该是 void* 类型并返回 rc,即 pthread_create() 的结果。

由于 pthread_create_with_stack 是一个 void 函数,并且它在定义中不返回任何内容,因此将 rc 设置为其返回值不仅没有意义,而且是一个错误 gcc/g++ 甚至不会让您尝试编译。

It's this line

rc = pthread_create_with_stack( &thread[t], BusyWork, t );

Your definition of pthread_create_with_stack is of type void. Should be of type void* and return rc, the result of pthread_create().

Since pthread_create_with_stack is a void function, and it returns nothing in the definition, setting rc to its return value is not only meaningless, it's an error gcc/g++ won't even let you try to compile.

所有深爱都是秘密 2024-12-19 13:45:23

是的,有一个错误。返回类型 void 表示该函数不返回任何值。您尝试将 pthread_create_with_stack 的返回值分配给局部变量,但没有可分配的返回值。

您应该将 pthread_create_with-stack 声明为返回 int,然后使其实际返回一个值:

int pthread_create_with-stack(...)
{
    ...
    return pthread_create(...);
}

Yes, there is an error. A return type of void means that the function returns no value. You're trying to assign the return value of pthread_create_with_stack to a local variable, but there is no return value to assign.

You should instead declare pthread_create_with-stack as returning an int, and then make it actually return a value:

int pthread_create_with-stack(...)
{
    ...
    return pthread_create(...);
}
乱了心跳 2024-12-19 13:45:23

您正在从 void 函数获取返回值并尝试将其分配给变量。 pthread_create_with_stack 不返回任何内容;不要分配它或使用它。

You're taking the return value from a void function and trying to assign it to a variable. pthread_create_with_stack doesn't return anything; don't assign it or use it.

拥抱影子 2024-12-19 13:45:23

您的 pthread_create_with_stack 是一个 void 函数。它不返回值。您不能将其结果分配给变量。

Your pthread_create_with_stack is a void function. It doesn't return a value. You can't assign it's result to a variable.

︶ ̄淡然 2024-12-19 13:45:23

pthread_create_with_stack 被定义为返回 void。

 rc = pthread_create_with_stack( &thread[t], BusyWork, t ); 

其中 rc 定义为 int 是不合法的

pthread_create_with_stack is defined as returning void.

 rc = pthread_create_with_stack( &thread[t], BusyWork, t ); 

where rc is defined as int is not legal

心的憧憬 2024-12-19 13:45:23

void 只是意味着“什么都没有”
你根本无法保存“什么”!

void simply means "nothing"
N you simply cant save "nothing"!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文