试图将ostream传递给函数并计算结果ostream
Trying to pass ostream to function and cout the resulting ostream
需求是创建一个流,将其传递给第二个函数进行修改(必须以这种方式传递,因为输出需要访问单独类的私有数据成员)。然后输出修改后的流。它按原样编译,但只返回MyStream的地址,我需要它来返回流。
/* Prints the details of the media collection. */
void MediaCollection::PrintMediaDetails() const{
ostream MyStream(NULL);
for(int i = 0; i < CurrentMCSize; i++){
TheCollection[i]->PrintMedia(MyStream);
cout << i + 1 <<" : "<<&MyStream;
}
}
/*Takes Stream and and returns data members*/
void Media::PrintMedia(ostream & Out) const{
Out<<Title<<" "<<Artist<<" "<<&RunningTime<<endl;
}
它正在打印MyStream的地址,因为你告诉它
改变<<&MyStream;
<<MyStream;
但是ostream MyStream(NULL)没有传递一个streambuf
就没有任何意义cpp-progger建议使用std::ostringstream。像这样定义一个
ostringstream MyStream;
并使用
打印内容cout << i + 1 <<" : "<< MyStream.str();
相关文章:
- C++ 模板类无法修复 ostream 和 istream 函数
- 关于 std::ostream 构造函数
- 我们如何将Ostream函数作为类的成员函数实现,而不是作为朋友函数,以便我可以用作虚拟函数
- 动态数组模板类:ostream&运算符友元函数的问题
- 在Ostream Overload Friend函数中使用向量
- 在ostream函数中使用字符串流
- C++ 多项式复制构造函数和 ostream 覆盖导致模因泄漏?
- 打印任何带有非成员ostream重载函数的c++11数组
- 在ostream函数中使用递归函数作为输出
- C ++使用类型为void或std::ostream的函数&do prints?
- 在C++中使用非友元方法重载 I/O 运算符 |&istream 和 &ostream 函数
- 没有合适的默认构造函数可用于初始化 ostream 指针
- 函数中的setw返回一个ostream
- 如何理解函数 ostream&operator<< (ostream& os, const unsigned char* s)
- Ostream友元函数的特化模板类
- 关于ostream以及如何在main函数中使用
- 定义ostream操作符的友元函数
- 重写std::ostream flush()函数,不被std::flush调用
- 如何使用"char *filename"在构造函数中初始化"ostream &out"
- 操作符重载- cout和cin——ostream函数,不能被引用——它是一个已删除的函数