簡體   English   中英

c++ 中的 ofstream 錯誤

[英]ofstream error in c++

我在 C++ 中收到流錯誤,這是我的代碼

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

來自 Dev-C++ 10 的錯誤

C:\devp\main.cpp 聚合 `std::ofstream OutStream' 的類型不完整,無法定義

提前致謝

你可以試試這個:

#include <fstream>

int main () {
  std::ofstream myfile;

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

  return 0;
}

文件流實際上是在<fstream>定義的。

您可能沒有包含適當的頭文件。

在源文件的開頭添加#include <fstream>應該可以修復錯誤。

我認為這是一個簡單的拼寫錯誤 offstream 而不是 ofstream。

可能,您包含了錯誤的頭文件。 有一個頭文件 <iosfwd> 用於需要從 STL 引用類型而不需要類型的完整聲明的頭文件。 您仍然需要包含正確的標頭 <iostream> 才能使用相關類型。

包含fstream頭文件就是這樣。 因為ofstream & ifstream包含在fstream 中。

我遇到了同樣的錯誤,因為我忘記使用using namespace std; 加入之后,問題就解決了。

添加 ======#include 庫,盡管我建議使用“使用命名空間 std;” 而不是“std::ofstream”

代碼:

#include #include

使用命名空間標准;

int main() {

ofstream myfile;
myfile.open("example.txt", ios::out);
myfile << "Writing this to a file\n";
myfile.close

return 0;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM