what is wrong with c++ streams when using boost.python?

假如想象 提交于 2019-12-03 23:37:15
H. Brandsmeier

I also run into this problem a while ago, using self_ns as outlined in the answers here Build problems when adding `__str__` method to Boost Python C++ class

The reason for using self_ns is explained by Dave himself here http://mail.python.org/pipermail/cplusplus-sig/2004-February/006496.html


Just for the sake of debugging, please try

inline std::string toString(const Bernoulli& b) 
{
   std::ostringstream s;
   s << b;
   return s.str(); 
}

And replace .def(self_ns::str(self)) with

class_<Bernoulli>("Bernoulli")
[...]
.def("__str__", &toString)

Have you tried using boost::format instead? Since you are already using boost, it shouldn't be too much of a hassle.

boost::format( "%d%%" ) % ( b.p() * 100.0 )

Another thing, try passing std::endl explicitly to the output os.

have you tried flushing the stream prior to using the .str() method?

oss << b.p() * 100.0 << "%" << std::flush;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!