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')
strcmpis for comparing string literals, not structs, and it takes two parameters, each a null-terminated string, and not one parameter. Do you know how to implement operator overloads in C++? What prompted you to, somehow, usestrcmphere?auto operator<=>(const tfecha&) const = default;and it will create all the comparisons for you. Ex on godboltoperator==()that does the comparison, since class/struct types do not have such an operator by default. How that operator will work is obviously up to the developer but it is reasonably typical that it will work by comparing all elements in some way.strcmp()is not a good way to compare anything other than what it is designed for i.e. a lexicographical comparison of two nul terminated arrays of char, so is not good for comparing instances of struct/class types.