gpt4 book ai didi

c++ - 类型错误和指针混淆

转载 作者:行者123 更新时间:2023-11-30 01:51:32 25 4
gpt4 key购买 nike

我正在尝试学习指针,但出现此错误。需要改头文件类Request吗?为什么我会收到这样的错误?

cannot convert `req' from type `Request' to type `Request *'

错误发生在这些行中:

//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);

代码:

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);
}
}

我的 request.h 文件包含以下内容:

class Request
{
public:
//constructor intializes request type

Request(char *u, int rqtID, int rqtrID);
char *url;
int requestID;
int requesterID;

最佳答案

您需要使用 address-of operator这里:

Request *reqq = &req; //req points to the object 
// -------------^

注意 & 在这种情况下 does not mean reference .

If the operand is an lvalue expression of some type T, operator& creates and returns a prvalue of type T*.

关于c++ - 类型错误和指针混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26048757/

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