1

I am trying to learn pointers and I get this error. Do I need to change the header file class Request? Why am I getting such an error?

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

The error is happening in theses lines:

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

code:

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

my request.h file consist of the following:

class Request
{
 public:
  //constructor intializes request type

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

}

monkey doodle
  • 690
  • 5
  • 12
  • 22

2 Answers2

3

You need to use the address-of operator here:

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

Note that & in this case does not mean reference.

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

Community
  • 1
  • 1
1

Put the reference of req using &req. A pointer type accept a pointer value, not an object.

Request *reqq = &req; //req points to the object
Nicolas Albert
  • 2,586
  • 1
  • 16
  • 16