Why does overloading operator<< to print Eigen class member result in a segfault?
Clash 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.
c++ eigen member cout
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.
add a comment |Â
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.
c++ eigen member cout
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 thestd::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
add a comment |Â
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.
c++ eigen member cout
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
c++ eigen member cout
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.
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 thestd::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
add a comment |Â
5
You must return thestd::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
add a comment |Â
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;
Or justreturn out << object.mat;
– john
48 mins ago
@john: Added your suggestion too. Thanks.
– P.W
45 mins ago
add a comment |Â
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;
Or justreturn out << object.mat;
– john
48 mins ago
@john: Added your suggestion too. Thanks.
– P.W
45 mins ago
add a comment |Â
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;
Or justreturn out << object.mat;
– john
48 mins ago
@john: Added your suggestion too. Thanks.
– P.W
45 mins ago
add a comment |Â
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;
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;
edited 42 mins ago
john
33.6k12645
33.6k12645
answered 55 mins ago
P.W
3,145221
3,145221
Or justreturn out << object.mat;
– john
48 mins ago
@john: Added your suggestion too. Thanks.
– P.W
45 mins ago
add a comment |Â
Or justreturn 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
add a comment |Â
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.
red is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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