变量'std::ofstream outfile'有初始化程序但是类型不完整。

4

我已经用Dev C++创建了一个关于这个问题的CPP程序,它可以创建很多文件。现在我又用Dev C++创建了一个程序,但是出现了错误。我的代码专门用于创建HTML模板,但是出现了错误

以下是我的一些代码

#include <iostream>
using std::cout;
using std::cin;
using std::ofstream;
using std::endl;

void HelloWorld() {
    ofstream outfile ("html1.html");

    outfile << "<html> " << endl;
    outfile << "   <head> <title> Template Hello World </title> </head>" << endl;
    outfile << "   <body> <h1> Hello World! </h1> </body> " << endl;
    outfile << "</html> " << endl;
    outfile << "// Template HTML 2018 " << endl;
    cout << "Created Succesfuly! Directory: Program's folder " << endl;


    outfile.close(); 
}

3
你忘记包括 ostream、ofstream 或其他类似的内容。 - Borgleader
8
请尝试使用 #include <fstream> - Galik
谢谢你们俩。那个管用了。我是C++的初学者。 - Spanish Mapping
1
@SpanishMapping 如果你是初学者,应该从一本好的C++书籍开始学习,而不是随意编码。 - Algirdas Preidžius
2
@AlgirdasPreidžius 谁说它是随机的? - Borgleader
我会这样做的。我刚刚使用了一些模板和视频进入了。 - Spanish Mapping
1个回答

5
只需在您的包含文件中添加#include <fstream>
#include <iostream>
#include <fstream>
using std::cout;
using std::cin;
using std::ofstream;
using std::endl;

void HelloWorld() {
    ofstream outfile ("html1.html");

    outfile << "<html> " << endl;
    outfile << "   <head> <title> Template Hello World </title> </head>" << endl;
    outfile << "   <body> <h1> Hello World! </h1> </body> " << endl;
    outfile << "</html> " << endl;
    outfile << "// Template HTML 2018 " << endl;
    cout << "Created Succesfuly! Directory: Program's folder " << endl;

    outfile.close();
}

int main()
{
    HelloWorld();
    return 0;
}

输出:

Created Successfully! Directory: Program's folder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接