Cpp
Cpp

Reputation: 29

How can I compare two struct instances?

My program compares two dates with the tda date. The operation must return the phrase "are same" or "are differen":

#include<iostream>
//#include<stdio.h>
#include<string.h> 

using namespace std;

struct tfecha {
    int dia;
    int mes;
    int anio;
};

int main()
{
    tfecha f1,f2;
    cout<<"Ingrese primera fecha:"<<endl;
    cin>>f1.dia;
    cin>>f1.mes;
    cin>>f1.anio;

    cout<<"Ingrese segunda fecha:"<<endl;
    cin>>f2.dia;
    cin>>f2.mes;
    cin>>f2.anio;

    if(strcmp(f1==f2) {
        cout<<"Las fechas son iguales:"<<endl;
    } else {
       cout<<"Las fechas son diferentes:"<<endl;
    }
}

However, I get the following error:

[Error] no match for 'operator ==' (operand types are 'tfecha' and 'tfecha')

Upvotes: 0

Views: 5511

Answers (4)

einpoklum
einpoklum

Reputation: 131525

With the recent version of the language standard, C++20, the language provides opt-in default comparison operators. So, in your case:

struct tfecha {
    int dia;
    int mes;
    int anio;
    bool operator==(const tfecha&) const = default;
};

will mean you can compare tfecha's.

To use C++20, you must invoke your C++ compiler with the switch -std=c++20 (for g++, clang++) or /std:c++20 (with MSVC), etc.

See it working on Godbolt.


Other notes:

  • Please don't write using namespace std:
    Why is "using namespace std;" considered bad practice?

  • You can't use strcmp() with the result of the comparison of the two tfechas.

  • You can use the following alternative syntax for defaulting the comparison operator:

      friend bool operator==(tfecha const&, tfecha const&) = default;
    

    this also works because in newer C++ versions you can define friend functions within the class body.

  • I don't want to encourage you to write in English. However, note that if your code is intended to be read by people not only in your home country - they are very likely to know some English (since C++ itself is specified in English and so are its keywords), less likely to know what the fields mean, and much less likely to realize that tefcha = t+fecha and that fecha means date in Castellano.

Upvotes: 6

Guillaume Racicot
Guillaume Racicot

Reputation: 41760

In C++20 you can now simply tell the compiler to generate comparison operators:

struct tfecha{
    int dia;
    int mes;
    int anio;

    friend auto operator<=>(tfecha const&, tfecha const&) = default;
    friend auto operator==(tfecha const&, tfecha const&) -> bool = default;
};including 

Then all comparisons will work, including order operator and not equal.

Upvotes: 0

Henry Gilbert
Henry Gilbert

Reputation: 13

So bassically you just have to overload the == operator as c++ doesn't know how to use it in regards to your classes, hence, you need to tell it how it use it. Try something like this:


inline bool operator==(const & tfecha lhs, const tfecha& rhs){ 
if(lhs.dia == rhs.dia && lhs.mes == rhs.mes && lhs.anio == rhs.anio){
return true;
}
else{
return false;
}
}

So you can see that your just checking if all the values are the same then we can say this is the same object.

Please note this is not technically the same object, and to check that you would just check the two objects' memory locations equal each other. IE. &lhs == &rhs.

Upvotes: 0

MikeCAT
MikeCAT

Reputation: 75062

You have to define operator== if you want to use that for your original class.

Also strcmp is for comparing C-style strings (sequences of characters terminated by a null-character), not structures.

#include<iostream>
//#include<stdio.h>
#include<string.h> 
using namespace std;
struct tfecha{
    int dia;
    int mes;
    int anio;

    // define operator==
    bool operator==(const tfecha& t) const {
        return dia == t.dia && mes == t.mes && anio == t.anio;
    }
};
int main()
{
    tfecha f1,f2;
    cout<<"Ingrese primera fecha:"<<endl;
    cin>>f1.dia;
    cin>>f1.mes;
    cin>>f1.anio;

    cout<<"Ingrese segunda fecha:"<<endl;
    cin>>f2.dia;
    cin>>f2.mes;
    cin>>f2.anio;

    // remove strcmp
    if(f1==f2){
      cout<<"Las fechas son iguales:"<<endl;
    }else
    {
       cout<<"Las fechas son diferentes:"<<endl;
    }
 }

Upvotes: 2

Related Questions