2014-03-19 18 views
-3

需求是创建一个流,将其传递给第二个函数进行修改(它必须以这种方式传递,因为输出需要访问私有数据成员单独的课程)。然后输出修改后的流。它按原样编译,但只返回MyStream的地址,我需要它返回流。试图通过ostream来运行和cout结果流ostream

/* 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; 
} 
+3

是否有问题打印的内容? – 0x499602D2

+0

有什么问题?它工作吗?它是如何工作的?我们不是预测者。 – zoska

+0

我想很清楚,特尼克想干什么。 他需要一个'std :: ostringstream',放入'PrintMedia'方法中的一些东西,然后输出'MyStream.str()'。 –

回答

0

正在打印MyStream的地址,因为你是它告诉给

变化

<<&MyStream; 

<<MyStream; 

但是ostream的MyStream(NULL)不作任何无passing it a streambuf

cpp-progger建议使用std :: ostringstream。定义一个像这样的

ostringstream MyStream; 

,并使用

cout << i + 1 <<" : "<< MyStream.str();