1
我试图在Qt中重载operator <<
。如何超载运算符<<
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
我在做什么错?
非常感谢!我是新手:) – MrElmar 2013-04-20 07:33:50