c++ - How to overload operator << -


i try overload operator << in 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;     = "qweer321";     hash2<<a;     qdebug() << "hash1: " << hash1->result();     qdebug() << "hash2: " << hash2->result(); } 

but error:

no match 'operator<<' in 'hash2 << a' 

i tried declare operator member of class, error.

error: 'mycryptographichash& mycryptographichash::operator<<(mycryptographichash&, const qstring&)' must take 1 argument 

what doing wrong?

your code should be

*hash2 << a; 

hash2 pointer, not object.

however in code posted there no obvious reason why hash2 pointer. write

{     mycryptographichash hash1(qcryptographichash::sha1);     mycryptographichash hash2(qcryptographichash::sha1);     hash1.adddata("abc1234");     qstring a;     = "qweer321";     hash2 << a;     qdebug() << "hash1: " << hash1.result();     qdebug() << "hash2: " << hash2.result(); } 

which have advantage of not leaking memory.

but maybe there's more code posted.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -