37

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

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

error from Dev-C++ 10

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

Thanks in advance

Bentoy13
  • 4,756
  • 1
  • 17
  • 32

8 Answers8

67

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;
}
Viet
  • 17,296
  • 32
  • 99
  • 134
32

The file streams are actually defined in <fstream>.

6

You may not be including the appropiate header file.

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

2

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

Naveen
  • 71,879
  • 44
  • 171
  • 229
  • Can you post the exact code you are compiling? OutStream is not defined in the code posted here. – Naveen Jun 29 '09 at 09:11
2

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.

1800 INFORMATION
  • 125,607
  • 29
  • 156
  • 237
0

Include fstream header file that's it. Because ofstream & ifstream are included in fstream.

-2

I faced the same error because I forgot to use using namespace std; after including it, the problem was solved.

Abhishek Dutt
  • 988
  • 5
  • 11
  • 21
-3

adding an ======#include library, although I recommend using "using namespace std;" instead of "std::ofstream"

Code:

#include #include

using namespace std;

int main() {

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

return 0;

}

  • Please format your code properly and see: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/10871073) – Adrian Mole Jul 17 '21 at 17:31