[英]C++ ostringstream strange behavior when chaining in cout is used
I am a C++ beginner ( came from Java ). 我是C ++初学者(来自Java)。 I have the following code: 我有以下代码:
//#include <boost/algorithm/string.hpp>
#include <iostream>
#include <math.h>
#include <vector>
#include <string.h>
#include <string>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <memory>
#include <assert.h>
#include <cctype>
using namespace std;
class Point{
private:
int x;
int y;
public:
Point(int x,int y){
this->x=x;
this->y=y;
}
int getX(){
return x;
}
int getY(){
return y;
}
operator const char*(){
return toString().c_str();
}
string toString(){
ostringstream stream;
stream<<"( "<<x<<", "<<y<<" )";
return stream.str();
}
};
class Line{
private:
Point p1=Point(0,0);
Point p2=Point(0,0);
public:
Line(Point p1, Point p2){
this->p1=p1;
this->p2=p2;
}
Point getP1(){
return p1;
}
Point getP2(){
return p2;
}
operator const char*(){
ostringstream stream;
stream<<"[ "<<p1<<" -> "<<p2<<" ]";
return stream.str().c_str();
}
// operator const char*(){
// ostringstream stream;
// stream<<"[ "<<p1<<" -> ";
// stream<<p2<<" ]";
// return stream.str().c_str();
// }
};
int main()
{
Line line=Line(Point(1,2), Point(3,4));
cout<<line<<endl;
cout<<"\nProgram exited successfully."<<endl;
return 0;
}
I have redefined the operator const* so that I can use cout< 我已经重新定义了运算符const *,以便可以使用cout <
But, If I run the program as it is now, with the second block commented out ( I have 2 versions of operator const*, and by default the second one is commented out ) ,it will display 但是,如果我按现在的样子运行程序,第二个代码块被注释掉(我有2个版本的const *操作符,默认情况下第二个代码被注释掉),它将显示
[ (1, 2) -> (1, 2) ] [(1,2)->(1,2)]
But when running with the second block uncommented, the output is as expected: 但是,当在第二个块未注释的情况下运行时,输出是预期的:
[ (1, 2) -> (3, 4) ] [(1,2)->(3,4)]
The issue seems to occur when I display both Point objects in the same line ( some kind of chaining, though I don't know if chaining is the right word here ) 当我在同一行中显示两个Point对象时,似乎出现了问题(某种形式的链接,尽管我不知道这里的链接是否正确)
My question, is,why is this happening? 我的问题是,为什么会这样?
I have added the std::ostream& operator << function to my Line class but now I'm receiving the following errors: 我已经将std :: ostream&运算符<<函数添加到我的Line类中,但是现在我收到以下错误:
/home/ryu/qt_workspace/hello/main.cpp:67: error: 'std::ostream& Line::operator<<(std::ostream&, const Line&)' must take exactly one argument
/home/ryu/qt_workspace/hello/main.cpp:77: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
Regards, Aurelian 问候,奥雷利亚
If you want to use cout <<
, there is a more direct way to do that. 如果要使用cout <<
,则有一种更直接的方法。
Add this function to Line
. 将此功能添加到Line
。
friend std::ostream& operator << ( std::ostream & os, const Line & l ){
os << "[ " << l.p1 << " -> " << l.p2 << " ]";
return os;
}
You should also note that your approach was returning invalid memory - this is a significant way that Java differs from C++. 您还应该注意,您的方法正在返回无效的内存-这是Java与C ++不同的重要方式。
return stream.str().c_str(); // Danger!
stream
was declared in operator const char*()
which limits its lifetime to that function. stream
是在operator const char*()
声明的,它将其生存期限制为该函数。 It is destroyed when that scope is exited. 退出该作用域时将销毁它。 As a result, you are returning a pointer to something that no longer exists. 结果,您将返回一个指针,该指针不再存在。
actually I think with C++11 returning the string by value is perfectly fine, so you can do the transfer there instead of using the cstring underneath. 实际上,我认为使用C ++ 11按值返回字符串是完全可以的,因此您可以在那里进行传输,而不是使用下面的cstring。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.