【发布时间】:2012-05-08 12:52:18
【问题描述】:
我正在尝试找到一种将 fout 或 cout 传递给函数的方法。我意识到有一些逻辑上简单的方法来处理这个问题,比如将 ifs 放在任何输出数据的函数中,甚至只是双向编写函数。然而,这似乎原始且低效。我不相信这段代码会起作用,我把它放在这里是为了确保很容易看到我“想要”做什么。请注意,我正在学习使用 C++ 的算法设计课程,我绝不是经验丰富的 C++ 程序员。我的课程仅限于使用您看到的标题。
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void helloWorld(char);
ofstream fout;
int main()
{
fout.open("coutfout.dat");
helloWorld(c);
helloWorld(f);
return 0;
}
void helloWorld(char x)
{
xout << "Hello World";
return;
}
【问题讨论】: