C++中的ofstream错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1057287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:36:16  来源:igfitidea点击:

ofstream error in c++

c++

提问by Viet

I am getting an ofstream error in C++, here is my code

我在 C++ 中遇到了一个 ofstream 错误,这是我的代码

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}

error from Dev-C++ 10

来自 Dev-C++ 10 的错误

C:\devp\main.cpp aggregate `std::ofstream OutStream' has incomplete type and cannot be defined

C:\devp\main.cpp 聚合 `std::ofstream OutStream' 类型不完整,无法定义

Thanks in advance

提前致谢

回答by Viet

You can try this:

你可以试试这个:

#include <fstream>

int main () {
  std::ofstream myfile;

  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();

  return 0;
}

回答by Viet

The file streams are actually defined in <fstream>.

文件流实际上是在<fstream>.

回答by Sebastián Mestre

You may not be including the appropiate header file.

您可能没有包含适当的头文件。

adding #include <fstream>at the beggining of your source file should fix the error.

#include <fstream>在源文件的beggining应该修正这个错误。

回答by Naveen

I think it is a simple spelling mistake offstream instead of ofstream.

我认为这是一个简单的拼写错误 offstream 而不是 ofstream。

回答by 1800 INFORMATION

Probably, you are including the wrong header file. There is a header <iosfwd> that is used for header files that need to reference types from the STL without needing a full declaration of the type. You still are required to include the proper header <iostream> in order to use the types in question.

可能,您包含了错误的头文件。有一个头文件 <iosfwd> 用于需要从 STL 引用类型而不需要类型的完整声明的头文件。您仍然需要包含正确的标头 <iostream> 才能使用相关类型。