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
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;
}
文件流实际上是在<fstream>
定义的。
You may not be including the appropiate header file.
adding #include <fstream>
at the beggining of your source file should fix the error.
我认为这是一个简单的拼写错误 offstream 而不是 ofstream。
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.
Include fstream
header file that's it. Because ofstream
& ifstream
are included in fstream.
I faced the same error because I forgot to use using namespace std;
after including it, the problem was solved.
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.