简体   繁体   中英

How can I compare two struct instances?

I have the following error in my program to compare two dates with the tda date. The operation must return the phrase "are same" or "are different".

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

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

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM