No match for 'Operator==' in find C function

Asked

Viewed 278 times

1

TipoApontador find (TipoItem x, TipoLista *lista){
   TipoApontador aux;
      if (lista->primeiro != NULL){
          aux = lista->primeiro;
          while (aux!=NULL){
             if (aux->item == x)
             return aux;
          aux = aux->prox;
          }
      }
  return NULL;
}

Guys, when I compile this function I created along with the code the compiler of the error in if(aux->item == x). Do you have any idea why ?

[Error] no match for 'Operator==' (operand types are 'Tipoitem' and 'Tipoitem')

  • That’s what’s written there in the error message. You need to know something else?

  • Yes, I didn’t really understand, I could explain ? , I modified, because I copied the wrong error msg

  • 2

    You changed the question. the problem now is that the types are incompatible, you can not buy oranges with bananas. But we don’t have information about the types to identify the exact problem. Oh, and you’re programming in C, but using a C compiler++.

  • I’ll post the whole code

  • And what do you want to do? It’s the problem I mentioned earlier, you can’t compare orange to banana, the solution is to take that comparison or switch to a comparison that tests equal things, but it depends on why you did it. If you have to look for an orange in a banana box in dynamic language you will find nothing, in static language you will say that you cannot look for it where you are looking, so the solution is to look for the same thing, but I do not know if that is your goal. The code as a whole is a bit confusing.

  • My goal in that part of the code is to check if the element exists in my queue, if it exists, returns the item, if it does not exist, returns null. Do you have any video or link that can help me to implement a dynamic queue in c ? Because to my understanding, I would be comparing two Tipoitem elements in that passage

  • @mahatt is important [Edit] and complements the original question with the details requested in the comments (and all else you can add). Here are some tips that may help you to elaborate the post and increase the chance of someone understanding your problem and elaborate answer: [Ask], [help] and [mcve].

Show 2 more comments

2 answers

6

The problem is exactly what the compiler tells you, who doesn’t know how to compare the two elements passed.

Consider a structure representing a Cartesian point on a 2d plane:

typedef struct {
   int x;
   int y;
} Ponto;

If we now try to create two points and compare with == won’t work:

int main(){
    Ponto p1 = {10, 20};
    Ponto p2 = {10, 20};

    if (p1 == p2){
        printf("iguais");
    }
    else {
        printf("diferentes");
    }

    return 0;
}

Because the compiler doesn’t know how to compare two points. Actually the error:

no match for ːOperator==' (operand types are ¡Ponto' and ːPonto')

It only happens if you are compiling in c++, if you are compiling in C as you should the error is different, although it means the same:

invalid operands to Binary == (have ːPonto {aka struct }' and ːPonto {aka struct }')

The way to solve in C is to compare the individual fields that represent equality for you. You can even do a function for that or maybe use memcmp to compare the whole memory, but in the end it will represent the same logic.

In my example comparing field to field would look like this:

if (p1.x == p2.x && p1.y == p2.y){
    printf("iguais");
}
else {
    printf("diferentes");
}

Compared to memcmp:

if (memcmp(&p1, &p2, sizeof(Ponto)) == 0){
    printf("iguais");
}
else {
    printf("diferentes");
}

In the latter he sees that he needs not only to pass the memory address of the two elements to be compared, as well as the amount of bytes to be compared in memory. The return is 0 when both memory blocks are equal, in the case when the two points are equal. It is also important to mention that you need the header <string.h> to the memcmp.

This type of comparisons is less common because it uses all the fields of the structure, which in many cases is not what is intended. And if any of the fields is a pointer the comparison checks if the pointer is equal or if it points to the same place and not if the value pointed is equal.

See these two examples in Ideone

2

On the line

if (aux->item == x)  

the expression aux->item is the type TipoItem, and the variable x is the type TipoCelula*. It is not possible to compare a Tipoitem (aux->item) structure with a Tipocelula* (aux) pointer, so the compiler complains.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.