0

По заданию есть

class Owner {
    char* name;
    char* surname;
    char* phone;
public:
Owner() {
        name = new char[12];
        strcpy(name, "noname");
        surname = new char[12];
        strcpy(surname, "nosurname");
        phone = new char[12];
        strcpy(phone, "nophone");
    };

... и тд... Надо перегрузить операторы ввода-вывода и == по всем полям Owner. Внутри public:

friend ostream& operator<<(ostream& out, const Owner& o) {
    out << o.name << " " << o.surname << " " << o.phone << endl;
    return out;
};
friend istream& operator>>(istream& in, Owner& o) {
    in >> o.name >> o.surname >> o.phone;
    return in;
};
friend bool operator==(const Owner &a, const Owner &b) {
    return (a.name == b.name && 
        a.surname == b.surname &&
        a.phone == b.phone);
};

В main():

Owner o1, o2("ABCD", "EFGH", "1234"), o3;
cout << ">> O3:" << endl;
cin >> o3;
cout << "<< O3:" << endl;
cout << o3;
cout << "O1 == O3:" << endl;
if (o1 == o3)
{
    cout << "O1 == O3" << endl;
}
else
{
    cout << "O1 != O3" << endl;
}

Постоянно выводит "O1 != O3", что бы я не вводил, даже когда noname nosurname nophone В чем может быть проблема?

1 Answers1

2
a.name == b.name

Это - сравнение указателей, которые, конечно же, разные у разных объектов...

С-строки сравнивают с помощью функций наподобие strcmp

Harry
  • 221,325