Why does overloading operator<< to print Eigen class member result in a segfault?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












For the following struct



struct TestClass 
TestClass() : mat(Eigen::Matrix3i::Zero())
Eigen::Matrix3i mat;
;


I would like to have an overloaded operator<< to print the mat member to std::cout. I tried



std::ostream& operator<<(std::ostream& out, const TestClass& object) 
out << object.mat;



This results in a segfault. Can anyone explain to me why?



A minimum working example:



#include <iostream>
#include <Eigen/Core>

struct TestClass
TestClass() : mat(Eigen::Matrix3i::Zero())
Eigen::Matrix3i mat;
;

std::ostream& operator<<(std::ostream& out, const TestClass& object)
out << object.mat;


int main()
TestClass testObject;

std::cout << testObject.mat << "nn"; // This works fine.
std::cout << testObject << 'n'; // This results in a segfault.

return 0;



I'm compiling with g++ version 7.3.0 and Eigen version 3.4 on Ubuntu 18.04.










share|improve this question







New contributor




red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 5




    You must return the std::ostream reference from the operator overload implementation, e.g. return out << object.mat;. Non-void functions not returning anything are usually tagged with some compiler warnings, it might be worth enabling these (e.g. -Wall -pedantic).
    – lubgr
    58 mins ago















up vote
7
down vote

favorite












For the following struct



struct TestClass 
TestClass() : mat(Eigen::Matrix3i::Zero())
Eigen::Matrix3i mat;
;


I would like to have an overloaded operator<< to print the mat member to std::cout. I tried



std::ostream& operator<<(std::ostream& out, const TestClass& object) 
out << object.mat;



This results in a segfault. Can anyone explain to me why?



A minimum working example:



#include <iostream>
#include <Eigen/Core>

struct TestClass
TestClass() : mat(Eigen::Matrix3i::Zero())
Eigen::Matrix3i mat;
;

std::ostream& operator<<(std::ostream& out, const TestClass& object)
out << object.mat;


int main()
TestClass testObject;

std::cout << testObject.mat << "nn"; // This works fine.
std::cout << testObject << 'n'; // This results in a segfault.

return 0;



I'm compiling with g++ version 7.3.0 and Eigen version 3.4 on Ubuntu 18.04.










share|improve this question







New contributor




red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 5




    You must return the std::ostream reference from the operator overload implementation, e.g. return out << object.mat;. Non-void functions not returning anything are usually tagged with some compiler warnings, it might be worth enabling these (e.g. -Wall -pedantic).
    – lubgr
    58 mins ago













up vote
7
down vote

favorite









up vote
7
down vote

favorite











For the following struct



struct TestClass 
TestClass() : mat(Eigen::Matrix3i::Zero())
Eigen::Matrix3i mat;
;


I would like to have an overloaded operator<< to print the mat member to std::cout. I tried



std::ostream& operator<<(std::ostream& out, const TestClass& object) 
out << object.mat;



This results in a segfault. Can anyone explain to me why?



A minimum working example:



#include <iostream>
#include <Eigen/Core>

struct TestClass
TestClass() : mat(Eigen::Matrix3i::Zero())
Eigen::Matrix3i mat;
;

std::ostream& operator<<(std::ostream& out, const TestClass& object)
out << object.mat;


int main()
TestClass testObject;

std::cout << testObject.mat << "nn"; // This works fine.
std::cout << testObject << 'n'; // This results in a segfault.

return 0;



I'm compiling with g++ version 7.3.0 and Eigen version 3.4 on Ubuntu 18.04.










share|improve this question







New contributor




red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











For the following struct



struct TestClass 
TestClass() : mat(Eigen::Matrix3i::Zero())
Eigen::Matrix3i mat;
;


I would like to have an overloaded operator<< to print the mat member to std::cout. I tried



std::ostream& operator<<(std::ostream& out, const TestClass& object) 
out << object.mat;



This results in a segfault. Can anyone explain to me why?



A minimum working example:



#include <iostream>
#include <Eigen/Core>

struct TestClass
TestClass() : mat(Eigen::Matrix3i::Zero())
Eigen::Matrix3i mat;
;

std::ostream& operator<<(std::ostream& out, const TestClass& object)
out << object.mat;


int main()
TestClass testObject;

std::cout << testObject.mat << "nn"; // This works fine.
std::cout << testObject << 'n'; // This results in a segfault.

return 0;



I'm compiling with g++ version 7.3.0 and Eigen version 3.4 on Ubuntu 18.04.







c++ eigen member cout






share|improve this question







New contributor




red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 1 hour ago









red

414




414




New contributor




red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 5




    You must return the std::ostream reference from the operator overload implementation, e.g. return out << object.mat;. Non-void functions not returning anything are usually tagged with some compiler warnings, it might be worth enabling these (e.g. -Wall -pedantic).
    – lubgr
    58 mins ago













  • 5




    You must return the std::ostream reference from the operator overload implementation, e.g. return out << object.mat;. Non-void functions not returning anything are usually tagged with some compiler warnings, it might be worth enabling these (e.g. -Wall -pedantic).
    – lubgr
    58 mins ago








5




5




You must return the std::ostream reference from the operator overload implementation, e.g. return out << object.mat;. Non-void functions not returning anything are usually tagged with some compiler warnings, it might be worth enabling these (e.g. -Wall -pedantic).
– lubgr
58 mins ago





You must return the std::ostream reference from the operator overload implementation, e.g. return out << object.mat;. Non-void functions not returning anything are usually tagged with some compiler warnings, it might be worth enabling these (e.g. -Wall -pedantic).
– lubgr
58 mins ago













1 Answer
1






active

oldest

votes

















up vote
9
down vote



accepted










The return value of the overloaded operator<< is std::ostream&. However, you are not returning anything from it.



Do the following:



out << object.mat;
return out;


or alternatively,



return out << object.mat;





share|improve this answer






















  • Or just return out << object.mat;
    – john
    48 mins ago











  • @john: Added your suggestion too. Thanks.
    – P.W
    45 mins ago










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',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






red is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52421478%2fwhy-does-overloading-operator-to-print-eigen-class-member-result-in-a-segfault%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
9
down vote



accepted










The return value of the overloaded operator<< is std::ostream&. However, you are not returning anything from it.



Do the following:



out << object.mat;
return out;


or alternatively,



return out << object.mat;





share|improve this answer






















  • Or just return out << object.mat;
    – john
    48 mins ago











  • @john: Added your suggestion too. Thanks.
    – P.W
    45 mins ago














up vote
9
down vote



accepted










The return value of the overloaded operator<< is std::ostream&. However, you are not returning anything from it.



Do the following:



out << object.mat;
return out;


or alternatively,



return out << object.mat;





share|improve this answer






















  • Or just return out << object.mat;
    – john
    48 mins ago











  • @john: Added your suggestion too. Thanks.
    – P.W
    45 mins ago












up vote
9
down vote



accepted







up vote
9
down vote



accepted






The return value of the overloaded operator<< is std::ostream&. However, you are not returning anything from it.



Do the following:



out << object.mat;
return out;


or alternatively,



return out << object.mat;





share|improve this answer














The return value of the overloaded operator<< is std::ostream&. However, you are not returning anything from it.



Do the following:



out << object.mat;
return out;


or alternatively,



return out << object.mat;






share|improve this answer














share|improve this answer



share|improve this answer








edited 42 mins ago









john

33.6k12645




33.6k12645










answered 55 mins ago









P.W

3,145221




3,145221











  • Or just return out << object.mat;
    – john
    48 mins ago











  • @john: Added your suggestion too. Thanks.
    – P.W
    45 mins ago
















  • Or just return out << object.mat;
    – john
    48 mins ago











  • @john: Added your suggestion too. Thanks.
    – P.W
    45 mins ago















Or just return out << object.mat;
– john
48 mins ago





Or just return out << object.mat;
– john
48 mins ago













@john: Added your suggestion too. Thanks.
– P.W
45 mins ago




@john: Added your suggestion too. Thanks.
– P.W
45 mins ago










red is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















red is a new contributor. Be nice, and check out our Code of Conduct.












red is a new contributor. Be nice, and check out our Code of Conduct.











red is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52421478%2fwhy-does-overloading-operator-to-print-eigen-class-member-result-in-a-segfault%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

List of Gilmore Girls characters

One-line joke