类型错误和指针混淆

Type error and pointer confusion

本文关键字:指针 错误 类型      更新时间:2023-10-16

我正在尝试学习指针,但我遇到了这个错误。我需要更改头文件类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] = '';
    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;

}

您需要在此处使用操作员的地址:

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

注意,在这种情况下&并不意味着引用。

如果操作数是某种类型T的左值表达式,则运算符&创建并返回类型为T*的prvalue。

使用&req放置req的引用。指针类型接受指针值,而不是对象。

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