12
votes
#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...?

3

3 Answers

13
votes

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

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)
3
votes

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.