クラスの従業員がいます
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class employee
{
public:
double operator + (employee);
istream& operator>> (istream&);
employee(int);
double getSalary();
private:
double salary;
};
int main()
{
employee A(400);
employee B(800);
employee C(220);
cin>>C;
}
employee::employee(int salary)
{
this->salary = salary;
}
double employee::operator + (employee e)
{
double total;
total = e.salary + this->salary;
return total;
}
double employee::getSalary()
{
return this->salary;
}
istream& employee::operator>> (istream& in)
{
in>>this->salary;
return in;
}
従業員オブジェクトを読み込むために入力演算子>>をオーバーロードしようとしていますが、次のエラーが発生します
std :: cinの演算子>>に一致しません
私は何が間違っているのですか?
編集:私は友達関数を介してそれを行う方法を知っています、私は今メンバー関数を介してそれを行う方法を学ぼうとしています
私は友達関数を介してそれを行う方法を知っています、私は今メンバー関数を介してそれを行う方法を学ぼうとしています
できません。
バイナリ用operator@
とオブジェクトA a
とB b
、構文がa @ b
コールするいずれかの形式の非メンバ関数operator@(A,B)
またはフォームのメンバ関数A::operator@(B)
。他には何もありません。
したがって、std::cin >> C
動作させるには、のメンバーである必要がありstd::istream
ますが、変更std::istream
できないためoperator>>
、メンバー関数として実装することはできません。
(あなたが奇妙で型破りで、書くC << std::cin
かC >> std::cin
、または書きたいのでなければ、他のプログラマーはあなたを混乱させて型破りであると嫌うでしょう。これをしないでください。)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加