私は、多くのファイル操作でC++でプログラムを開発しています。私は共通のヘッダーにstatic ofstreamを定義して、プロジェクトのどこにでもアクセスできるようにしました。コードの構造は以下の通りです:共通変数はすべてcom.hで定義されています。test.hとtest.cppはOPClassというクラスです。main.cppはメインプログラムを運びます。static ofstreamが動作しない理由
COM.H:
#ifndef __CLCOM__
#define __CLCOM__
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
static ofstream out;
static stringstream ss;
#endif
TEST.H:
#ifndef __CL__
#define __CL__
#include <iostream>
#include <fstream>
#include "com.h"
using namespace std;
class OPClass
{
public:
void run(void);
void show(ostream &o) const;
};
#endif
TEST.CPP:
#include "com.h"
#include "test.h"
void OPClass::run(void)
{
out << "Here is run()" << endl;
show(out);
}
void OPClass::show(ostream &o) const
{
o << "hello!" << endl;
}
MAIN.CPP:あなたが見ることができるように
#include "com.h"
#include "test.h"
void runmain(void)
{
OPClass op;
out.open("output.txt", ios::out | ios::trunc);
out << endl << "State changed!" << endl;
op.run();
if (out.is_open()) out.close();
}
int main(int argc, char* argv[])
{
runmain();
return 0;
}
、静的ofstreamのはアウトと命名した、メインプログラムとクラスで呼び出されます。私はmingw32を使用していて、コンパイル時や実行時に何の問題も見られませんでした。しかし、runmain()の情報だけが出力ファイルに書き込まれるようです。クラス内のそのファイルに書き込まれたその他のメッセージは、出力ファイルには表示されません。それはなぜですか?また、共通のファイルストリームを作成して、プロジェクトのどこでもそのファイルストリームにアクセスできますか?ありがとう。
ありがとうございます。できます。私はちょうど1つの質問を持っています、なぜヘッダーに "using"ステートメントを入れてはいけないのですか?とにかく、代わりに実装(つまりcpp)に入れることができますか?ありがとう – user1285419
よろしくお願いします。 'using'は実装ファイルでは問題ありませんが、ヘッダーでは悪い習慣とみなされています...なぜそれが当てはまるのかについてのQ&Aです:http://stackoverflow.com/questions/2232496/is-it-wrong-to-use- c-using-a-header-file – HostileFork