简体   繁体   中英

the object has type qualifiers that are not compatible with the member function

#include<iostream>

using namespace std;

class PhoneNumber

{

    int areacode;
    int localnum;
public:

    PhoneNumber();
    PhoneNumber(const int, const int);
    void display() const;
    bool valid() const;
    void set(int, int);
    PhoneNumber& operator=(const PhoneNumber& no);
    PhoneNumber(const PhoneNumber&);
};

istream& operator>>(istream& is, const PhoneNumber& no);


istream& operator>>(istream& is, const PhoneNumber& no)
{

    int area, local;
    cout << "Area Code     : ";
    is >> area;
    cout << "Local number  : ";
    is >> local;
    no.set(area, local);
    return is;
}

at no.set(area, local); it says that "the object has type qualifiers that are not compatible with the member function"

what should i do...?

You're passing no as const , but you try to modify it.

istream& operator>>(istream& is, const PhoneNumber& no)
//-------------------------------^
{

    int area, local;
    cout << "Area Code     : ";
    is >> area;
    cout << "Local number  : ";
    is >> local;
    no.set(area, local); // <------
    return is;
}

Your set method is not const (nor should it be), but you're attempting to call it on a const object.

Remove the const from the parameter to operator >> :

istream& operator>>(istream& is, PhoneNumber& no)

In the operator >> there is the second parameter with type const PhoneNumber& no that is it is a constant object, But you are trying to change it using member function set. For const objects you may call only member functions that have qualifier const.

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