【发布时间】:2013-04-20 07:20:40
【问题描述】:
我尝试在 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
我做错了什么?
【问题讨论】:
-
非常感谢!我是新手 :)
标签: c++ windows qt operators operator-overloading