Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;使用cout中的链接时的ostringstream奇怪行为 我是一个C++初学者(来自java)。我有以下代码: //#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; } /#包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 类点{ 私人: int x; int-y; 公众: 点(整数x,整数y){ 这个->x=x; 这->y=y; } int getX(){ 返回x; } int getY(){ 返回y; } 运算符常量字符*(){ 返回字符串().c_str(); } 字符串toString(){ ostringstream; stream_C++_Ostringstream - Fatal编程技术网 x=x; 这->y=y; } int getX(){ 返回x; } int getY(){ 返回y; } 运算符常量字符*(){ 返回字符串().c_str(); } 字符串toString(){ ostringstream; stream,c++,ostringstream,C++,Ostringstream" /> x=x; 这->y=y; } int getX(){ 返回x; } int getY(){ 返回y; } 运算符常量字符*(){ 返回字符串().c_str(); } 字符串toString(){ ostringstream; stream,c++,ostringstream,C++,Ostringstream" />

C++;使用cout中的链接时的ostringstream奇怪行为 我是一个C++初学者(来自java)。我有以下代码: //#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; } /#包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 类点{ 私人: int x; int-y; 公众: 点(整数x,整数y){ 这个->x=x; 这->y=y; } int getX(){ 返回x; } int getY(){ 返回y; } 运算符常量字符*(){ 返回字符串().c_str(); } 字符串toString(){ ostringstream; stream

C++;使用cout中的链接时的ostringstream奇怪行为 我是一个C++初学者(来自java)。我有以下代码: //#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; } /#包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 类点{ 私人: int x; int-y; 公众: 点(整数x,整数y){ 这个->x=x; 这->y=y; } int getX(){ 返回x; } int getY(){ 返回y; } 运算符常量字符*(){ 返回字符串().c_str(); } 字符串toString(){ ostringstream; stream,c++,ostringstream,C++,Ostringstream,如果您想使用cout实际上,我认为使用C++11按值返回字符串是非常好的,因此您可以在那里进行传输,而不是使用下面的cstring 提供打印功能的正确方法是重载运算符。关于无效指针,请详细说明一下好吗?您是指因为ostringstream是一个局部变量,我从中调用str,然后调用c_str,当声明方法结束时,指针也会被销毁?有没有办法知道方法使用动态或静态分配的指针?此外,如果指针无效,为什么程序不会崩溃,或者打印奇怪的字符?出于善意,您可以调用str(),这将创建一个std::string,然

如果您想使用
cout实际上,我认为使用C++11按值返回字符串是非常好的,因此您可以在那里进行传输,而不是使用下面的cstring


提供打印功能的正确方法是重载
运算符。关于无效指针,请详细说明一下好吗?您是指因为ostringstream是一个局部变量,我从中调用str,然后调用c_str,当声明方法结束时,指针也会被销毁?有没有办法知道方法使用动态或静态分配的指针?此外,如果指针无效,为什么程序不会崩溃,或者打印奇怪的字符?出于善意,您可以调用
str()
,这将创建一个
std::string
,然后调用
c_str())
并返回结果指针,该指针指向在完整表达式末尾被销毁的临时字符串。它不会崩溃或任何事情,因为它是未定义的行为。它可以做任何事情。关于您的更新-听起来您好像忘记了我答案中的
friend
关键字。嗨,Drew,很抱歉回答晚了,我ied添加了friend关键字,但我收到/home/ryu/qt_workspace/hello/main.cpp:81:错误:无法将'std::basic_ostream'左值绑定到'std::basic_ostream&&'谢谢您的支持。
str()
返回的字符串是。甚至
auto str=str.str()。c_str();
使
str
成为一个悬空指针。
/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>&&'
friend std::ostream& operator << ( std::ostream & os, const Line & l ){
    os << "[ " << l.p1 << " -> " << l.p2 << " ]";
    return os;
}
    return stream.str().c_str();  // Danger!