Class 从c+中的类成员函数写入文件+; void树::打印树(节点*cur){ 如果(当前->左){ 打印树(当前->左侧); } outFile getValue(); 如果(当前->右侧){ 打印树(当前->右侧); }

Class 从c+中的类成员函数写入文件+; void树::打印树(节点*cur){ 如果(当前->左){ 打印树(当前->左侧); } outFile getValue(); 如果(当前->右侧){ 打印树(当前->右侧); },class,ostream,member-functions,Class,Ostream,Member Functions,} 我需要从一个类的成员函数写入一个文件。但是,输出流对象是在我的主函数中声明的,并且我得到一个错误,表示outFile未在此范围内声明。我尝试将一个ostream对象作为参数传递,但没有成功。有什么建议吗 我尝试将ostream对象作为参数传递: void Tree::printTree(Node* cur){ if(cur->left){ printTree(cur->left); } outFile << cur->getValue(); if(cur

}


我需要从一个类的成员函数写入一个文件。但是,输出流对象是在我的主函数中声明的,并且我得到一个错误,表示outFile未在此范围内声明。我尝试将一个ostream对象作为参数传递,但没有成功。有什么建议吗

我尝试将ostream对象作为参数传递:

void Tree::printTree(Node* cur){
if(cur->left){
    printTree(cur->left);
}
outFile << cur->getValue();
if(cur->right){
    printTree(cur->right);
}
void树::打印树(节点*cur、ostream和输出){
如果(当前->左){
打印树(cur->left,&output);
}
输出getValue();
如果(当前->右侧){
打印树(当前->右侧,&输出);
}
}

我得到一个错误,说ostream尚未声明,即使我包括


编辑:已修复,我在.cpp中包含了iostream,但在.h中没有。

“我尝试将ostream对象作为参数传递”-显示您的最佳尝试,以及您遇到的编译器错误/警告。
std::ostream
?我使用的是命名空间std,这应该包括它,对吗?您要么没有包括它,要么没有使用命名空间
它。当您尝试
std::ostream
时会发生什么?
void Tree::printTree(Node* cur, ostream &output){
if(cur->left){
    printTree(cur->left, &output);
}
output << cur->getValue();
if(cur->right){
    printTree(cur->right, &output);
}