为什么静态流不起作用
why static ofstream doesn't work
我正在用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() 中的信息才会写入输出文件。写入类中该文件的任何其他消息永远不会出现在输出文件中。为什么会这样,我怎样才能编写一个通用的文件流,以便项目中的任何地方都可以访问它?谢谢。
每个编译单元都有自己的ss
和out
。 因此,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
将其绑定到它所在的文件的本地范围。
- C++实用程序::转换在静态链接库中不起作用
- 针对静态库的 G++ 链接不起作用
- 强制链接静态库与 cmake 不起作用
- 静态使用 make_unique 和 make_shared不起作用
- 当应用于静态方法时,为什么constexpr属性不起作用
- CMake:使静态库有效,但动态库不起作用
- 在声明节点创建链接列表时,为什么静态内存分配不起作用
- Boost Logger静态链接:不起作用
- 静态控制滚动条不起作用 Win32
- 称为 C 静态库,所有内容都包装在外部"C"中,但它仍然不起作用
- 通过JNI调用简单的java静态方法是不起作用的,尽管c++编译并运行它
- 静态函数调用不起作用 (C++)
- 如果我在静态成员中使用,Qt tr() 不起作用
- 定位在静态控制中不起作用
- 为什么其中一个模板静态函数有效,而另一个不起作用
- 为什么静态流不起作用
- OpenCV和Visual Studio 2015:为什么包含静态库不起作用
- 静态私有变量在c++中不起作用
- 在 c++ 中实现静态构造函数不起作用
- 为什么这个静态断言不起作用