gpt4 book ai didi

c++ - 错误 : ANSI C++ forbids implicit conversion from `void *' in assignment

转载 作者:行者123 更新时间:2023-11-28 02:40:50 24 4
gpt4 key购买 nike

我收到此错误消息,但我似乎不明白。ANSI C++ 禁止在赋值中从“void *”隐式转换是什么意思? .而 Fork 函数只需要函数名和一个数字

线程::Fork(VoidFunctionPtr func, int arg)

错误信息:

../threads/threadtest.cc: In function `void ServerThread(int)':
../threads/threadtest.cc:72: ANSI C++ forbids implicit conversion from `void *' in assignment
../threads/threadtest.cc:78: implicit declaration of function `int WorkerThread(...)'

地区:

72 - 78:

  nextReq = list -> Remove();


//check till the end
while (nextReq != NULL)
{
WorkerThread(&nextReq);

代码:

#include "copyright.h"
#include "system.h"
#include <stdio.h>
#include "request.h"

extern void serve(char *url);
//GLOBAL VARIABLE LIST
List *list;

//----------------------------------------------------------------------
// ThreadTest
// read file and serve urls
//----------------------------------------------------------------------

void
ClientThread(int request)
{
const int sz = 50;
char url[sz];

FILE *fp = fopen("url.txt", "r");
if (!fp)
printf(" Cannot open file url.txt!\n");
else {
int pos = 0;
char c = getc(fp);
while (c != EOF || pos == sz - 1) {
if (c == '\n') {
url[pos] = '\0';
serve(url);
pos = 0;

//Store necessary information in a Request object for each request.
Request req(url, request, 1);

Request *reqq = &req; //req points to the object
list->Append(reqq);
}
else {
url[pos++] = c;
}
c = getc(fp);
}
fclose(fp);
}
}

//----------------------------------------------------------------------

void
ServerThread(int which)
{

Request *nextReq;
//gets the first node off the list
nextReq = list -> Remove();


//check till the end
while (nextReq != NULL)
{
WorkerThread(nextReq);

}


}

//----------------------------------------------------------------------

void
WorkerThread (Request req)
{
serve(req.url);
currentThread -> Yield();
}

//----------------------------------------------------------------------

void
ThreadTest()
{
DEBUG('t', "Entering SimpleTest");
printf("THREAD TEST");

//Thread *c = new Thread("client thread");
Thread *s = new Thread("server thread");

s->Fork(ServerThread, 1);
ClientThread(0);

}

最佳答案

这似乎是违规行之一:

nextReq = list -> Remove();

看来 list->Remove()返回 void * . C++ 需要 强制转换以将其转换为另一个指针(C 不需要)。所以将其更改为:

nextReq = static_cast<Request *>(list -> Remove());

(或者,考虑将 List 设为模板类,这样您就可以避免这些类型的不安全转换。根据您的代码,STL 类 std::queue<Request> 应该可以满足您的需求。)

第二条违规线路是您调用 WorkerThread() 的电话在定义之前。您需要在定义 ServerThread() 之前为函数添加原型(prototype).否则编译器不知道它的原型(prototype)是什么,一旦到达 ServerThread() 的真正定义,它应该报错。它与之前推导出的原型(prototype)不匹配。

void WorkerThread(Request);

void
ServerThread(int which)
{
// ...

(或者,由于 WorkerThread() 没有调用 ServerThread() ,您可以交换两个函数定义的顺序来解决问题。)


此外,请注意这段代码是错误的:

Request req(url, request, 1);

Request *reqq = &req; //req points to the object
list->Append(reqq);

您构造一个对象,然后将指向堆栈分配对象的指针压入列表。当ClientThread()返回时,这个对象将被销毁,你留下一个指向不再存在的对象的指针。使用此指针将触发未定义的行为。考虑改为分配一个新的 Request在堆上使用 Request *reqq = new Request(url, request, 1); (但不要忘记在处理后 delete 对象)。

或者,更好的是,使用 std::queue<Request>正如我之前建议的那样——那么你就可以 queue.emplace(url, request, 1); .但请注意,您确实需要一种方法来同步从多个线程访问队列。

关于c++ - 错误 : ANSI C++ forbids implicit conversion from `void *' in assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26051472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com