64,647
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include <Windows.h>
#include <sstream>
#include <tchar.h>
#include <string>
using namespace std;
void main()
{
//这样得到的结果是错误的。
//输出的是一个地址,而不是字符串
ostringstream test;
test.operator<<("this is a test string");
cout<<( std::string(test.str()).c_str() )<<endl;
//这样就正确
ostringstream test2;
test2<<("this is a test string");
cout<<( std::string(test2.str()).c_str() )<<endl;
/*
//想请问为什么直接使用<< 和 使用 .operator<<() 会得到
//不同的结果,因为我想继承ostringstream重载<<
//必须调用基类的ostringstream::operator<<()方法
但结果却出错了。希望大家给点意见,谢谢!
class Newstringstream : public std::ostringstream
{
public:
ostringstream& operator << (POINT &point)
{
//这里出错了
ostringstream::operator<<
(_T("("))<<(point.x)<<(_T(","))<<(point.y)<<(_T(")"));
return *this;
}
};
*/
}
ostringstream test;
operator<<(test,"this is a test string");
cout<<( std::string(test.str()).c_str() )<<endl;
ostream& operator<< (bool& val );
ostream& operator<< (short& val );
ostream& operator<< (unsigned short& val );
ostream& operator<< (int& val );
ostream& operator<< (unsigned int& val );
ostream& operator<< (long& val );
ostream& operator<< (unsigned long& val );
ostream& operator<< (float& val );
ostream& operator<< (double& val );
ostream& operator<< (long double& val );
ostream& operator<< (const void* val );
ostream& operator<< (streambuf* sb );
ostream& operator<< (ostream& ( *pf )(ostream&));
ostream& operator<< (ios& ( *pf )(ios&));
ostream& operator<< (ios_base& ( *pf )(ios_base&));
*** the following functions are not members but GLOBAL functions:
//下面才是输出字符串相关的函数:
ostream& operator<< (ostream& out, char c );
ostream& operator<< (ostream& out, signed char c );
ostream& operator<< (ostream& out, unsigned char c );
ostream& operator<< (ostream& out, const char* s );
ostream& operator<< (ostream& out, const signed char* s );
ostream& operator<< (ostream& out, const unsigned char* s );