Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++11 compile failure #10

Closed
richmattes opened this issue Aug 14, 2016 · 1 comment · Fixed by #12
Closed

C++11 compile failure #10

richmattes opened this issue Aug 14, 2016 · 1 comment · Fixed by #12

Comments

@richmattes
Copy link
Contributor

When building hokuyoaist with a C++11 compiler, there are a lot of errors of the following variety:

/home/rich/fedora/hokuyoaist/hokuyoaist-b87a88aa66853d3c9d901d4e6be729c5fe69aae0/src/hokuyo_errors.cpp: In member function ‘virtual const char* hokuyoaist::BaseError::what() const’:
/home/rich/fedora/hokuyoaist/hokuyoaist-b87a88aa66853d3c9d901d4e6be729c5fe69aae0/src/hokuyo_errors.cpp:386:8: error: no match for ‘operator<<’ (operand types are ‘const stringstream {aka const std::__cxx11::basic_stringstream<char>}’ and ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’)
     ss << std::string(error_type_, sizeof(error_type_)) << " (" << desc_code_ << "): " <<
     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6.1.1/istream:39:0,
                 from /usr/include/c++/6.1.1/sstream:38,
                 from /home/rich/fedora/hokuyoaist/hokuyoaist-b87a88aa66853d3c9d901d4e6be729c5fe69aae0/include/hokuyoaist/hokuyo_errors.h:32,
                 from /home/rich/fedora/hokuyoaist/hokuyoaist-b87a88aa66853d3c9d901d4e6be729c5fe69aae0/src/hokuyo_errors.cpp:29:
/usr/include/c++/6.1.1/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(__ostream_type& (*__pf)(__ostream_type&))
       ^~~~~~~~
/usr/include/c++/6.1.1/ostream:108:7: note:   no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/6.1.1/ostream:117:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
       operator<<(__ios_type& (*__pf)(__ios_type&))
       ^~~~~~~~
/usr/include/c++/6.1.1/ostream:117:7: note:   no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) {aka std::basic_ios<char>& (*)(std::basic_ios<char>&)}’
/usr/include/c++/6.1.1/ostream:127:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(ios_base& (*__pf) (ios_base&))
       ^~~~~~~~
/usr/include/c++/6.1.1/ostream:127:7: note:   no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/include/c++/6.1.1/ostream:166:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(long __n)
       ^~~~~~~~
/usr/include/c++/6.1.1/ostream:166:7: note:   no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘long int’
/usr/include/c++/6.1.1/ostream:170:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(unsigned long __n)
       ^~~~~~~~
/usr/include/c++/6.1.1/ostream:170:7: note:   no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘long unsigned int’
/usr/include/c++/6.1.1/ostream:174:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(bool __n)
       ^~~~~~~~
...

I think the root cause is that issue #6 changed the what() function prototype for the exceptions in hokuyo_exception to be a const function as required by c++11 std::exception::what(). However, the function body is not const, as each of the what() functions modify the member std::stringstream ss variable.

To fix this, it seems like the error description that is being built in what() should be built in the exception constructor, and stored in ss_str. Then the what() function can just return ss_str.c_str(). Doing that could allow you to remove the stringstream ss from the class entirely in favor of a temporary stringstream in each constructor.

@jwakely
Copy link

jwakely commented Jan 28, 2017

std::exception::what() has always been const in C++, that's not new in C++11. Why is it not const in this code?!

gbiggs added a commit that referenced this issue Feb 15, 2017
Const-ify exception what() method. Fixes #10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants