![](/img/trans.png)
[英]Object has type qualifiers that are not compatible with the member function
[英]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(地區,當地); 它說“對象具有與成員函數不兼容的類型限定符”
我該怎么辦...?
你no
傳遞const
作為const
,但你試圖修改它。
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;
}
你的set
方法不是const
(也不應該是),但是你試圖在const
對象上調用它。
從參數中移除const
到operator >>
:
istream& operator>>(istream& is, PhoneNumber& no)
在運算符>>中有第二個參數類型為const PhoneNumber&no,它是一個常量對象,但是您正在嘗試使用成員函數集更改它。 對於const對象,您可以只調用具有限定符const的成員函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.