为什么静态流不起作用

why static ofstream doesn't work

本文关键字:不起作用 静态 为什么      更新时间:2023-10-16

我正在用c ++开发一个程序,其中包含大量文件io操作。我在公共标头中定义了一个静态流,以便可以在项目中的任何位置访问它。代码的结构如下:所有公共变量在 com.h、test.h 和 test 中定义.cpp用于一个名为 OPClass 的类,main.cpp 携带主程序

.COM。H:

#ifndef __CLCOM__
#define __CLCOM__
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
static ofstream out;
static stringstream ss;
#endif

测试。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

测试。.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;
}

主要。.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 被命名为 out,并将在主程序和类中调用。我正在使用 mingw32,在编译或运行时没有看到任何问题。但似乎只有 runmain() 中的信息才会写入输出文件。写入类中该文件的任何其他消息永远不会出现在输出文件中。为什么会这样,我怎样才能编写一个通用的文件流,以便项目中的任何地方都可以访问它?谢谢。

每个编译单元都有自己的ssout。 因此,main.cpp 看到的它们的实例与 test.cpp 看到的实例不同。

你在这里真的不需要静态。 要解决此问题,您只需使用 extern 关键字对变量及其分配进行原型设计,而不是在头文件中声明变量及其分配。

#ifndef __CLCOM__
#define __CLCOM__
#include <sstream>
#include <fstream>
#include <iostream>
// Note: don't put "using" statements in headers
// use fully qualified names instead 
extern std::ofstream out;
extern std::stringstream ss;
#endif

实际把你的声明放在哪里取决于你,只要确保它只在一个地方。 这可以是一个 com.cpp 文件,或者你可以把它贴在 main 中.cpp如果这适合你的项目。

std::ofstream out;
std::stringstream ss;

无论如何,并不是说像这样的全局变量是一个好主意......

先发制人的声明:你应该接受@HostileFork的回答。

就像附录一样,显示正在发生的事情的一种简单方法是在尝试使用它时打印出out的地址。

如果添加以下几个语句:

void OPClass::run(void)
{
  cout << "Address of 'out' = " << &out << endl;
  out << "Here is run()" << endl;
  show(out);
}

和:

void runmain(void)
{
  cout << "Address of 'out' = " << &out << endl; 
  OPClass op;
  out.open("output.txt", ios::out | ios::trunc);
  out << endl << "State changed!" << endl;
  op.run();
  if (out.is_open()) out.close();
}

您会注意到,out的两个 print 语句显示两个不同的地址。 这应该告诉您,您实际上得到了两个out实例创建为两个不同的变量。 OPClass中的方法正在尝试写入完全不同的输出流。 它与您在全球环境中使用static的方式有关;它的行为不像你想象的那样。 在全局上下文中,声明某些内容static将其绑定到它所在的文件的本地范围。