我尝试operator <<
在 Qt 中重载。
class MyCryptographicHash : public QCryptographicHash
{
public:
MyCryptographicHash(Algorithm method);
void addData(const QString &data );
friend MyCryptographicHash& operator<< (MyCryptographicHash &obj, const QString &value);
private:
QByteArray _data;
};
MyCryptographicHash& operator<<(MyCryptographicHash &obj, const QString &value) {
obj.addData(value);
return obj;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
MyCryptographicHash *hash1 = new MyCryptographicHash(QCryptographicHash::Sha1);
MyCryptographicHash *hash2 = new MyCryptographicHash(QCryptographicHash::Sha1);
hash1->addData("abc1234");
QString a;
a = "qweer321";
hash2<<a;
qDebug() << "HASH1: " << hash1->result();
qDebug() << "HASH2: " << hash2->result();
}
但我得到错误:
no match for 'operator<<' in 'hash2 << a'
我试图将运算符声明为类的成员,但也得到了错误。
error: 'MyCryptographicHash& MyCryptographicHash::operator<<(MyCryptographicHash&, const QString&)' must take exactly one argument
我究竟做错了什么?