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

标签 c++ pointers typeerror

我正在尝试学习指针,但出现此错误。需要改头文件类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/

相关文章:

javascript - 无法读取 null 的属性 'push'

c++ - 静态库、链接和依赖

c++ - 非动态分配的复制构造函数

c - 操作系统如何知道将不同的指针递增多少?

c - 将二维数组传递给函数

javascript - "TypeError: jsdom.jsdom is not a function"与 npm paper 模块中的 paper-node.js

c++ - 复杂形状匹配的最佳方法是什么

c++ - 使用 getline 从文本文件创建 2 个并行字符串数组

通过使用指针移动 +1 来加密和解密字符串

python - 类型错误:字符串索引必须是整数,而不是 Pt[] 中的 str