Can`t overload the operator << for a my own class












-2















I am trying to reduce code redundancy using the overload of an output operator to a stream instead of print functions.



//***.h
class MainWind : public QWidget
{
Q_OBJECT

public:
explicit MainWind(QWidget *parent = nullptr);
~MainWind();
MainWind *operator<<(const QString &str);
private:
Ui::MainWind *ui;
};

//***.cpp
MainWind *MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return this;
}


At this moment everything compiles successfully.



But when I try to use:



//other.cpp
MainWind *mainWind = new MainWind;
mainWind << QString("str");


I got this error:




ServerSocket.cpp:39: error: invalid operands to binary expression
('MainWind *' and 'QString')
qstring.h:1410: candidate function not viable: no known conversion from >'MainWind *' to 'QDataStream &' for 1st argument



...



And there are a lot of candidates for this position)




Or



//other.cpp
MainWind *mainWind = new MainWind;
mainWind <<"str";


I got this error:




ServerSocket.cpp:39: error: invalid operands to binary expression
('MainWind *' and 'const char [4]') ServerSocket.cpp:39: error:
invalid operands of types 'MainWind*' and 'const char [4]' to binary
'operator<<'
CurrentSession::inst().mainWind() << "str";



                                                  ^



What could be the problem?



ADDITION TO THIS QUESTION:



Attempt to use:



//*.h
friend MainWind *operator<<(MainWind *out,QString &str);

//***.cpp
MainWind * operator<<(MainWind *out, QString &str)
{
out->ui->serverConsole->insertPlainText(str);
return out;
}


Compilation of previous code is successful.



According to the idea, if the first operand could not be a pointer, this code would not compile...



But when using this:



//other.cpp
MainWind *mW = new MainWind;
mW << "str";


Compilation go to error:




ServerSocket.cpp:37: error: invalid operands of types 'MainWind*' and 'const char [4]' to binary 'operator<<'
mW << "str";



                ^










share|improve this question

























  • The problem is that you defined the << overload an a MainWind, and the left-hand operand of this << operator is not a MainWind. It is a MainWind *.

    – Sam Varshavchik
    Jan 2 at 1:08
















-2















I am trying to reduce code redundancy using the overload of an output operator to a stream instead of print functions.



//***.h
class MainWind : public QWidget
{
Q_OBJECT

public:
explicit MainWind(QWidget *parent = nullptr);
~MainWind();
MainWind *operator<<(const QString &str);
private:
Ui::MainWind *ui;
};

//***.cpp
MainWind *MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return this;
}


At this moment everything compiles successfully.



But when I try to use:



//other.cpp
MainWind *mainWind = new MainWind;
mainWind << QString("str");


I got this error:




ServerSocket.cpp:39: error: invalid operands to binary expression
('MainWind *' and 'QString')
qstring.h:1410: candidate function not viable: no known conversion from >'MainWind *' to 'QDataStream &' for 1st argument



...



And there are a lot of candidates for this position)




Or



//other.cpp
MainWind *mainWind = new MainWind;
mainWind <<"str";


I got this error:




ServerSocket.cpp:39: error: invalid operands to binary expression
('MainWind *' and 'const char [4]') ServerSocket.cpp:39: error:
invalid operands of types 'MainWind*' and 'const char [4]' to binary
'operator<<'
CurrentSession::inst().mainWind() << "str";



                                                  ^



What could be the problem?



ADDITION TO THIS QUESTION:



Attempt to use:



//*.h
friend MainWind *operator<<(MainWind *out,QString &str);

//***.cpp
MainWind * operator<<(MainWind *out, QString &str)
{
out->ui->serverConsole->insertPlainText(str);
return out;
}


Compilation of previous code is successful.



According to the idea, if the first operand could not be a pointer, this code would not compile...



But when using this:



//other.cpp
MainWind *mW = new MainWind;
mW << "str";


Compilation go to error:




ServerSocket.cpp:37: error: invalid operands of types 'MainWind*' and 'const char [4]' to binary 'operator<<'
mW << "str";



                ^










share|improve this question

























  • The problem is that you defined the << overload an a MainWind, and the left-hand operand of this << operator is not a MainWind. It is a MainWind *.

    – Sam Varshavchik
    Jan 2 at 1:08














-2












-2








-2


1






I am trying to reduce code redundancy using the overload of an output operator to a stream instead of print functions.



//***.h
class MainWind : public QWidget
{
Q_OBJECT

public:
explicit MainWind(QWidget *parent = nullptr);
~MainWind();
MainWind *operator<<(const QString &str);
private:
Ui::MainWind *ui;
};

//***.cpp
MainWind *MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return this;
}


At this moment everything compiles successfully.



But when I try to use:



//other.cpp
MainWind *mainWind = new MainWind;
mainWind << QString("str");


I got this error:




ServerSocket.cpp:39: error: invalid operands to binary expression
('MainWind *' and 'QString')
qstring.h:1410: candidate function not viable: no known conversion from >'MainWind *' to 'QDataStream &' for 1st argument



...



And there are a lot of candidates for this position)




Or



//other.cpp
MainWind *mainWind = new MainWind;
mainWind <<"str";


I got this error:




ServerSocket.cpp:39: error: invalid operands to binary expression
('MainWind *' and 'const char [4]') ServerSocket.cpp:39: error:
invalid operands of types 'MainWind*' and 'const char [4]' to binary
'operator<<'
CurrentSession::inst().mainWind() << "str";



                                                  ^



What could be the problem?



ADDITION TO THIS QUESTION:



Attempt to use:



//*.h
friend MainWind *operator<<(MainWind *out,QString &str);

//***.cpp
MainWind * operator<<(MainWind *out, QString &str)
{
out->ui->serverConsole->insertPlainText(str);
return out;
}


Compilation of previous code is successful.



According to the idea, if the first operand could not be a pointer, this code would not compile...



But when using this:



//other.cpp
MainWind *mW = new MainWind;
mW << "str";


Compilation go to error:




ServerSocket.cpp:37: error: invalid operands of types 'MainWind*' and 'const char [4]' to binary 'operator<<'
mW << "str";



                ^










share|improve this question
















I am trying to reduce code redundancy using the overload of an output operator to a stream instead of print functions.



//***.h
class MainWind : public QWidget
{
Q_OBJECT

public:
explicit MainWind(QWidget *parent = nullptr);
~MainWind();
MainWind *operator<<(const QString &str);
private:
Ui::MainWind *ui;
};

//***.cpp
MainWind *MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return this;
}


At this moment everything compiles successfully.



But when I try to use:



//other.cpp
MainWind *mainWind = new MainWind;
mainWind << QString("str");


I got this error:




ServerSocket.cpp:39: error: invalid operands to binary expression
('MainWind *' and 'QString')
qstring.h:1410: candidate function not viable: no known conversion from >'MainWind *' to 'QDataStream &' for 1st argument



...



And there are a lot of candidates for this position)




Or



//other.cpp
MainWind *mainWind = new MainWind;
mainWind <<"str";


I got this error:




ServerSocket.cpp:39: error: invalid operands to binary expression
('MainWind *' and 'const char [4]') ServerSocket.cpp:39: error:
invalid operands of types 'MainWind*' and 'const char [4]' to binary
'operator<<'
CurrentSession::inst().mainWind() << "str";



                                                  ^



What could be the problem?



ADDITION TO THIS QUESTION:



Attempt to use:



//*.h
friend MainWind *operator<<(MainWind *out,QString &str);

//***.cpp
MainWind * operator<<(MainWind *out, QString &str)
{
out->ui->serverConsole->insertPlainText(str);
return out;
}


Compilation of previous code is successful.



According to the idea, if the first operand could not be a pointer, this code would not compile...



But when using this:



//other.cpp
MainWind *mW = new MainWind;
mW << "str";


Compilation go to error:




ServerSocket.cpp:37: error: invalid operands of types 'MainWind*' and 'const char [4]' to binary 'operator<<'
mW << "str";



                ^







c++ qt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 2:46







Vlad

















asked Jan 2 at 1:05









VladVlad

84




84













  • The problem is that you defined the << overload an a MainWind, and the left-hand operand of this << operator is not a MainWind. It is a MainWind *.

    – Sam Varshavchik
    Jan 2 at 1:08



















  • The problem is that you defined the << overload an a MainWind, and the left-hand operand of this << operator is not a MainWind. It is a MainWind *.

    – Sam Varshavchik
    Jan 2 at 1:08

















The problem is that you defined the << overload an a MainWind, and the left-hand operand of this << operator is not a MainWind. It is a MainWind *.

– Sam Varshavchik
Jan 2 at 1:08





The problem is that you defined the << overload an a MainWind, and the left-hand operand of this << operator is not a MainWind. It is a MainWind *.

– Sam Varshavchik
Jan 2 at 1:08












3 Answers
3






active

oldest

votes


















1















//other.cpp
MainWind *mainWind = new MainWind;
mainWind <<"str";



The reason is that mainWind << "str" looks for an operator<<() that accepts two arguments, the first of which is a MainWind *.



Whereas, you have defined a MainWind::operator<<() which is called with the first argument a MainWind &. There is no direct conversion from a MainWind * to a MainWind & (or to a MainWind). Hence the error message.



One way to get the code to compile is to change mainWind <<"str" to *mainWind << "str". The * dereferences the pointer, and produces a reference, which is what your operator<<() expects.



The catch is then that



*mainWind << "str" << "another str";


will not compile, since it is equivalent to



(*mainWind).operator<<("str") << "another str";


where (*mainWind).operator<<("str") returns a MainWind *. This causes the same problem (again) when trying to stream "another str".



The real fix is to change operator<<() so it returns a reference



//  within the class definition of MainWind

MainWind &operator<<(const QString &str);

// definition of the operator<<()

MainWind &MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


and change the calling code to either



//other.cpp version 2

MainWind *mainWind = new MainWind;
*mainWind <<"str";

// this will work too

*mainWind << "str" << "another str";

// note reliance on cleaning up mainWind to avoid a memory leak

delete mainWind;


There is no other fix that would allow you to use mainWind << "str" since overloading non-member operator<<() is only permitted on class or enumerated types, not on pointers.






share|improve this answer


























  • A lot of text makes little sense ... How do I overload the function as in addition to my question?

    – Vlad
    Jan 2 at 1:59











  • @Vlad - I've edited to address that.

    – Peter
    Jan 2 at 2:00











  • I would like to using the pointer without the "*". Therefore, I want to overload the function.

    – Vlad
    Jan 2 at 2:00











  • @Vlad - my edit addresses that too.

    – Peter
    Jan 2 at 2:03











  • Your example of function overload still result error: MainWind *mW = new MainWind; mW << "str"; This is for your last insight? Maybe I do not use it correctly?

    – Vlad
    Jan 2 at 2:09





















5














You need to use *mainWind << QString("str");. The LHS has to be an object, not a pointer.



While at it, I strongly recommend changing the operator<< function to return a reference to the object, not a pointer.



MainWind& operator<<(const QString &str);


and the implementation to



MainWind& MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


That will allow you to chain the operator.



*mainWind << QString("str") << QString("Second string");





share|improve this answer


























  • This is fine, but I need to work with exactly the pointer, but the function overloading given above gives to me a result similar to the past.

    – Vlad
    Jan 2 at 1:51













  • @Vlad, using pointers instead of objects is a choice. I am sure it is not a requirement. Morever, using pointers with the << operators is not idiomatic. Just something to think about.

    – R Sahu
    Jan 2 at 3:30



















3














You overloaded << on MainWnd not MainWnd*.



*mainWind << QString("str");



Also you want QString const&






share|improve this answer


























  • And if I want to use the pointer yet? without the "*" at the beginning. Is it possible? How should I change the overload?

    – Vlad
    Jan 2 at 1:13













  • @Vlad, you can use mainWind->operator<<(QString("str"));

    – R Sahu
    Jan 2 at 1:17











  • @RSahu , This is code redundancy. Nobody does it

    – Vlad
    Jan 2 at 1:30













  • @Vlad, that's why you should use *mainWind << ...

    – R Sahu
    Jan 2 at 1:32






  • 1





    @Vlad, try making the second argument QString const& str.

    – R Sahu
    Jan 2 at 1:43











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000183%2fcant-overload-the-operator-for-a-my-own-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















//other.cpp
MainWind *mainWind = new MainWind;
mainWind <<"str";



The reason is that mainWind << "str" looks for an operator<<() that accepts two arguments, the first of which is a MainWind *.



Whereas, you have defined a MainWind::operator<<() which is called with the first argument a MainWind &. There is no direct conversion from a MainWind * to a MainWind & (or to a MainWind). Hence the error message.



One way to get the code to compile is to change mainWind <<"str" to *mainWind << "str". The * dereferences the pointer, and produces a reference, which is what your operator<<() expects.



The catch is then that



*mainWind << "str" << "another str";


will not compile, since it is equivalent to



(*mainWind).operator<<("str") << "another str";


where (*mainWind).operator<<("str") returns a MainWind *. This causes the same problem (again) when trying to stream "another str".



The real fix is to change operator<<() so it returns a reference



//  within the class definition of MainWind

MainWind &operator<<(const QString &str);

// definition of the operator<<()

MainWind &MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


and change the calling code to either



//other.cpp version 2

MainWind *mainWind = new MainWind;
*mainWind <<"str";

// this will work too

*mainWind << "str" << "another str";

// note reliance on cleaning up mainWind to avoid a memory leak

delete mainWind;


There is no other fix that would allow you to use mainWind << "str" since overloading non-member operator<<() is only permitted on class or enumerated types, not on pointers.






share|improve this answer


























  • A lot of text makes little sense ... How do I overload the function as in addition to my question?

    – Vlad
    Jan 2 at 1:59











  • @Vlad - I've edited to address that.

    – Peter
    Jan 2 at 2:00











  • I would like to using the pointer without the "*". Therefore, I want to overload the function.

    – Vlad
    Jan 2 at 2:00











  • @Vlad - my edit addresses that too.

    – Peter
    Jan 2 at 2:03











  • Your example of function overload still result error: MainWind *mW = new MainWind; mW << "str"; This is for your last insight? Maybe I do not use it correctly?

    – Vlad
    Jan 2 at 2:09


















1















//other.cpp
MainWind *mainWind = new MainWind;
mainWind <<"str";



The reason is that mainWind << "str" looks for an operator<<() that accepts two arguments, the first of which is a MainWind *.



Whereas, you have defined a MainWind::operator<<() which is called with the first argument a MainWind &. There is no direct conversion from a MainWind * to a MainWind & (or to a MainWind). Hence the error message.



One way to get the code to compile is to change mainWind <<"str" to *mainWind << "str". The * dereferences the pointer, and produces a reference, which is what your operator<<() expects.



The catch is then that



*mainWind << "str" << "another str";


will not compile, since it is equivalent to



(*mainWind).operator<<("str") << "another str";


where (*mainWind).operator<<("str") returns a MainWind *. This causes the same problem (again) when trying to stream "another str".



The real fix is to change operator<<() so it returns a reference



//  within the class definition of MainWind

MainWind &operator<<(const QString &str);

// definition of the operator<<()

MainWind &MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


and change the calling code to either



//other.cpp version 2

MainWind *mainWind = new MainWind;
*mainWind <<"str";

// this will work too

*mainWind << "str" << "another str";

// note reliance on cleaning up mainWind to avoid a memory leak

delete mainWind;


There is no other fix that would allow you to use mainWind << "str" since overloading non-member operator<<() is only permitted on class or enumerated types, not on pointers.






share|improve this answer


























  • A lot of text makes little sense ... How do I overload the function as in addition to my question?

    – Vlad
    Jan 2 at 1:59











  • @Vlad - I've edited to address that.

    – Peter
    Jan 2 at 2:00











  • I would like to using the pointer without the "*". Therefore, I want to overload the function.

    – Vlad
    Jan 2 at 2:00











  • @Vlad - my edit addresses that too.

    – Peter
    Jan 2 at 2:03











  • Your example of function overload still result error: MainWind *mW = new MainWind; mW << "str"; This is for your last insight? Maybe I do not use it correctly?

    – Vlad
    Jan 2 at 2:09
















1












1








1








//other.cpp
MainWind *mainWind = new MainWind;
mainWind <<"str";



The reason is that mainWind << "str" looks for an operator<<() that accepts two arguments, the first of which is a MainWind *.



Whereas, you have defined a MainWind::operator<<() which is called with the first argument a MainWind &. There is no direct conversion from a MainWind * to a MainWind & (or to a MainWind). Hence the error message.



One way to get the code to compile is to change mainWind <<"str" to *mainWind << "str". The * dereferences the pointer, and produces a reference, which is what your operator<<() expects.



The catch is then that



*mainWind << "str" << "another str";


will not compile, since it is equivalent to



(*mainWind).operator<<("str") << "another str";


where (*mainWind).operator<<("str") returns a MainWind *. This causes the same problem (again) when trying to stream "another str".



The real fix is to change operator<<() so it returns a reference



//  within the class definition of MainWind

MainWind &operator<<(const QString &str);

// definition of the operator<<()

MainWind &MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


and change the calling code to either



//other.cpp version 2

MainWind *mainWind = new MainWind;
*mainWind <<"str";

// this will work too

*mainWind << "str" << "another str";

// note reliance on cleaning up mainWind to avoid a memory leak

delete mainWind;


There is no other fix that would allow you to use mainWind << "str" since overloading non-member operator<<() is only permitted on class or enumerated types, not on pointers.






share|improve this answer
















//other.cpp
MainWind *mainWind = new MainWind;
mainWind <<"str";



The reason is that mainWind << "str" looks for an operator<<() that accepts two arguments, the first of which is a MainWind *.



Whereas, you have defined a MainWind::operator<<() which is called with the first argument a MainWind &. There is no direct conversion from a MainWind * to a MainWind & (or to a MainWind). Hence the error message.



One way to get the code to compile is to change mainWind <<"str" to *mainWind << "str". The * dereferences the pointer, and produces a reference, which is what your operator<<() expects.



The catch is then that



*mainWind << "str" << "another str";


will not compile, since it is equivalent to



(*mainWind).operator<<("str") << "another str";


where (*mainWind).operator<<("str") returns a MainWind *. This causes the same problem (again) when trying to stream "another str".



The real fix is to change operator<<() so it returns a reference



//  within the class definition of MainWind

MainWind &operator<<(const QString &str);

// definition of the operator<<()

MainWind &MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


and change the calling code to either



//other.cpp version 2

MainWind *mainWind = new MainWind;
*mainWind <<"str";

// this will work too

*mainWind << "str" << "another str";

// note reliance on cleaning up mainWind to avoid a memory leak

delete mainWind;


There is no other fix that would allow you to use mainWind << "str" since overloading non-member operator<<() is only permitted on class or enumerated types, not on pointers.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 2:12

























answered Jan 2 at 1:52









PeterPeter

27.9k32157




27.9k32157













  • A lot of text makes little sense ... How do I overload the function as in addition to my question?

    – Vlad
    Jan 2 at 1:59











  • @Vlad - I've edited to address that.

    – Peter
    Jan 2 at 2:00











  • I would like to using the pointer without the "*". Therefore, I want to overload the function.

    – Vlad
    Jan 2 at 2:00











  • @Vlad - my edit addresses that too.

    – Peter
    Jan 2 at 2:03











  • Your example of function overload still result error: MainWind *mW = new MainWind; mW << "str"; This is for your last insight? Maybe I do not use it correctly?

    – Vlad
    Jan 2 at 2:09





















  • A lot of text makes little sense ... How do I overload the function as in addition to my question?

    – Vlad
    Jan 2 at 1:59











  • @Vlad - I've edited to address that.

    – Peter
    Jan 2 at 2:00











  • I would like to using the pointer without the "*". Therefore, I want to overload the function.

    – Vlad
    Jan 2 at 2:00











  • @Vlad - my edit addresses that too.

    – Peter
    Jan 2 at 2:03











  • Your example of function overload still result error: MainWind *mW = new MainWind; mW << "str"; This is for your last insight? Maybe I do not use it correctly?

    – Vlad
    Jan 2 at 2:09



















A lot of text makes little sense ... How do I overload the function as in addition to my question?

– Vlad
Jan 2 at 1:59





A lot of text makes little sense ... How do I overload the function as in addition to my question?

– Vlad
Jan 2 at 1:59













@Vlad - I've edited to address that.

– Peter
Jan 2 at 2:00





@Vlad - I've edited to address that.

– Peter
Jan 2 at 2:00













I would like to using the pointer without the "*". Therefore, I want to overload the function.

– Vlad
Jan 2 at 2:00





I would like to using the pointer without the "*". Therefore, I want to overload the function.

– Vlad
Jan 2 at 2:00













@Vlad - my edit addresses that too.

– Peter
Jan 2 at 2:03





@Vlad - my edit addresses that too.

– Peter
Jan 2 at 2:03













Your example of function overload still result error: MainWind *mW = new MainWind; mW << "str"; This is for your last insight? Maybe I do not use it correctly?

– Vlad
Jan 2 at 2:09







Your example of function overload still result error: MainWind *mW = new MainWind; mW << "str"; This is for your last insight? Maybe I do not use it correctly?

– Vlad
Jan 2 at 2:09















5














You need to use *mainWind << QString("str");. The LHS has to be an object, not a pointer.



While at it, I strongly recommend changing the operator<< function to return a reference to the object, not a pointer.



MainWind& operator<<(const QString &str);


and the implementation to



MainWind& MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


That will allow you to chain the operator.



*mainWind << QString("str") << QString("Second string");





share|improve this answer


























  • This is fine, but I need to work with exactly the pointer, but the function overloading given above gives to me a result similar to the past.

    – Vlad
    Jan 2 at 1:51













  • @Vlad, using pointers instead of objects is a choice. I am sure it is not a requirement. Morever, using pointers with the << operators is not idiomatic. Just something to think about.

    – R Sahu
    Jan 2 at 3:30
















5














You need to use *mainWind << QString("str");. The LHS has to be an object, not a pointer.



While at it, I strongly recommend changing the operator<< function to return a reference to the object, not a pointer.



MainWind& operator<<(const QString &str);


and the implementation to



MainWind& MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


That will allow you to chain the operator.



*mainWind << QString("str") << QString("Second string");





share|improve this answer


























  • This is fine, but I need to work with exactly the pointer, but the function overloading given above gives to me a result similar to the past.

    – Vlad
    Jan 2 at 1:51













  • @Vlad, using pointers instead of objects is a choice. I am sure it is not a requirement. Morever, using pointers with the << operators is not idiomatic. Just something to think about.

    – R Sahu
    Jan 2 at 3:30














5












5








5







You need to use *mainWind << QString("str");. The LHS has to be an object, not a pointer.



While at it, I strongly recommend changing the operator<< function to return a reference to the object, not a pointer.



MainWind& operator<<(const QString &str);


and the implementation to



MainWind& MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


That will allow you to chain the operator.



*mainWind << QString("str") << QString("Second string");





share|improve this answer















You need to use *mainWind << QString("str");. The LHS has to be an object, not a pointer.



While at it, I strongly recommend changing the operator<< function to return a reference to the object, not a pointer.



MainWind& operator<<(const QString &str);


and the implementation to



MainWind& MainWind::operator<<(const QString &str)
{
ui->serverConsole->insertPlainText(str);
return *this;
}


That will allow you to chain the operator.



*mainWind << QString("str") << QString("Second string");






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 1:46

























answered Jan 2 at 1:10









R SahuR Sahu

168k1292191




168k1292191













  • This is fine, but I need to work with exactly the pointer, but the function overloading given above gives to me a result similar to the past.

    – Vlad
    Jan 2 at 1:51













  • @Vlad, using pointers instead of objects is a choice. I am sure it is not a requirement. Morever, using pointers with the << operators is not idiomatic. Just something to think about.

    – R Sahu
    Jan 2 at 3:30



















  • This is fine, but I need to work with exactly the pointer, but the function overloading given above gives to me a result similar to the past.

    – Vlad
    Jan 2 at 1:51













  • @Vlad, using pointers instead of objects is a choice. I am sure it is not a requirement. Morever, using pointers with the << operators is not idiomatic. Just something to think about.

    – R Sahu
    Jan 2 at 3:30

















This is fine, but I need to work with exactly the pointer, but the function overloading given above gives to me a result similar to the past.

– Vlad
Jan 2 at 1:51







This is fine, but I need to work with exactly the pointer, but the function overloading given above gives to me a result similar to the past.

– Vlad
Jan 2 at 1:51















@Vlad, using pointers instead of objects is a choice. I am sure it is not a requirement. Morever, using pointers with the << operators is not idiomatic. Just something to think about.

– R Sahu
Jan 2 at 3:30





@Vlad, using pointers instead of objects is a choice. I am sure it is not a requirement. Morever, using pointers with the << operators is not idiomatic. Just something to think about.

– R Sahu
Jan 2 at 3:30











3














You overloaded << on MainWnd not MainWnd*.



*mainWind << QString("str");



Also you want QString const&






share|improve this answer


























  • And if I want to use the pointer yet? without the "*" at the beginning. Is it possible? How should I change the overload?

    – Vlad
    Jan 2 at 1:13













  • @Vlad, you can use mainWind->operator<<(QString("str"));

    – R Sahu
    Jan 2 at 1:17











  • @RSahu , This is code redundancy. Nobody does it

    – Vlad
    Jan 2 at 1:30













  • @Vlad, that's why you should use *mainWind << ...

    – R Sahu
    Jan 2 at 1:32






  • 1





    @Vlad, try making the second argument QString const& str.

    – R Sahu
    Jan 2 at 1:43
















3














You overloaded << on MainWnd not MainWnd*.



*mainWind << QString("str");



Also you want QString const&






share|improve this answer


























  • And if I want to use the pointer yet? without the "*" at the beginning. Is it possible? How should I change the overload?

    – Vlad
    Jan 2 at 1:13













  • @Vlad, you can use mainWind->operator<<(QString("str"));

    – R Sahu
    Jan 2 at 1:17











  • @RSahu , This is code redundancy. Nobody does it

    – Vlad
    Jan 2 at 1:30













  • @Vlad, that's why you should use *mainWind << ...

    – R Sahu
    Jan 2 at 1:32






  • 1





    @Vlad, try making the second argument QString const& str.

    – R Sahu
    Jan 2 at 1:43














3












3








3







You overloaded << on MainWnd not MainWnd*.



*mainWind << QString("str");



Also you want QString const&






share|improve this answer















You overloaded << on MainWnd not MainWnd*.



*mainWind << QString("str");



Also you want QString const&







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 3:08

























answered Jan 2 at 1:08









Yakk - Adam NevraumontYakk - Adam Nevraumont

187k21199384




187k21199384













  • And if I want to use the pointer yet? without the "*" at the beginning. Is it possible? How should I change the overload?

    – Vlad
    Jan 2 at 1:13













  • @Vlad, you can use mainWind->operator<<(QString("str"));

    – R Sahu
    Jan 2 at 1:17











  • @RSahu , This is code redundancy. Nobody does it

    – Vlad
    Jan 2 at 1:30













  • @Vlad, that's why you should use *mainWind << ...

    – R Sahu
    Jan 2 at 1:32






  • 1





    @Vlad, try making the second argument QString const& str.

    – R Sahu
    Jan 2 at 1:43



















  • And if I want to use the pointer yet? without the "*" at the beginning. Is it possible? How should I change the overload?

    – Vlad
    Jan 2 at 1:13













  • @Vlad, you can use mainWind->operator<<(QString("str"));

    – R Sahu
    Jan 2 at 1:17











  • @RSahu , This is code redundancy. Nobody does it

    – Vlad
    Jan 2 at 1:30













  • @Vlad, that's why you should use *mainWind << ...

    – R Sahu
    Jan 2 at 1:32






  • 1





    @Vlad, try making the second argument QString const& str.

    – R Sahu
    Jan 2 at 1:43

















And if I want to use the pointer yet? without the "*" at the beginning. Is it possible? How should I change the overload?

– Vlad
Jan 2 at 1:13







And if I want to use the pointer yet? without the "*" at the beginning. Is it possible? How should I change the overload?

– Vlad
Jan 2 at 1:13















@Vlad, you can use mainWind->operator<<(QString("str"));

– R Sahu
Jan 2 at 1:17





@Vlad, you can use mainWind->operator<<(QString("str"));

– R Sahu
Jan 2 at 1:17













@RSahu , This is code redundancy. Nobody does it

– Vlad
Jan 2 at 1:30







@RSahu , This is code redundancy. Nobody does it

– Vlad
Jan 2 at 1:30















@Vlad, that's why you should use *mainWind << ...

– R Sahu
Jan 2 at 1:32





@Vlad, that's why you should use *mainWind << ...

– R Sahu
Jan 2 at 1:32




1




1





@Vlad, try making the second argument QString const& str.

– R Sahu
Jan 2 at 1:43





@Vlad, try making the second argument QString const& str.

– R Sahu
Jan 2 at 1:43


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000183%2fcant-overload-the-operator-for-a-my-own-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith