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