【问题标题】:variable 'std::ofstream outfile' has initializer but incomplete type变量“std::ofstream outfile”具有初始化程序但类型不完整
【发布时间】:2018-10-25 13:52:51
【问题描述】:

我已经为此编写了一个CPP 程序,它使用Dev C++ 创建了很多文件,这个也是使用Dev C++ 的,但现在它显示错误。我的code 专门用于创建HTML 模板,但它显示error

这是我的一些code

#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(); 
}

【问题讨论】:

  • 您忘记包含 ostream 或 ofstream 或 wtv
  • 试试#include &lt;fstream&gt;
  • 感谢两位。那行得通。我是 C++ 的初学者,所以
  • @SpanishMapping 如果你是初学者,你应该向good C++ book学习,而不是随机编码。
  • @AlgirdasPreidžius 谁说这是随机的?

标签: c++ dev-c++


【解决方案1】:

只需将#include &lt;fstream&gt; 添加到您的包含:

#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-11
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多