C++ 创建 std::ofstream 对象时“不允许使用不完整的类型”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28882683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:05:50  来源:igfitidea点击:

"Incomplete type not allowed " when creating std::ofstream objects

c++visual-studio-2013fstreamofstream

提问by arun49 vs

Visual Studio Throws this Strange Error:

Visual Studio 抛出这个奇怪的错误:

Incomplete type not allowed

不允许不完整的类型

When I try to create an std::ofstream object. Here is the code I wrote inside a function.

当我尝试创建 std::ofstream 对象时。这是我在函数中编写的代码。

void OutPutLog()
{
     std::ofstream outFile("Log.txt");
}

whenever it encounters this Visual Studio throws that Error. Why This Happens?

每当遇到此 Visual Studio 时都会引发该错误。为什么会这样?

回答by HostileFork says dont trust SE

As @Mgetz says, you probably forgot to #include <fstream>.

正如@Mgetz 所说,您可能忘记了#include <fstream>.

The reason you didn't get a not declarederror and instead this incomplete type not allowederror has to do with what happens when there is a type that has been "forward declared", but not yet fully defined.

您没有收到not declared错误的原因,而是该incomplete type not allowed错误与存在已“向前声明”但尚未完全定义的类型时发生的情况有关。

Look at this example:

看这个例子:

#include <iostream>

struct Foo; // "forward declaration" for a struct type

void OutputFoo(Foo & foo); // another "forward declaration", for a function

void OutputFooPointer(Foo * fooPointer) {
    // fooPointer->bar is unknown at this point...
    // we can still pass it by reference (not by value)
    OutputFoo(*fooPointer);
}

struct Foo { // actual definition of Foo
    int bar;
    Foo () : bar (10) {} 
};

void OutputFoo(Foo & foo) {
    // we can mention foo.bar here because it's after the actual definition
    std::cout << foo.bar;
}

int main() {
    Foo foo; // we can also instantiate after the definition (of course)
    OutputFooPointer(&foo);
}

Notice we could not actually instantiate a Foo object or refer its contents until afterthe real definition. When we only have the forward declaration available, we may only talk about it by pointer or reference.

请注意在真正定义之前我们无法实际实例化 Foo 对象或引用其内容。当我们只有前向声明可用时,我们可能只能通过指针或引用来谈论它。

What is likely happening is you included some iostream header that had forward-declared std::ofstreamin a similar way. But the actual definition of std::ofstreamis in the <fstream>header.

可能发生的情况是您包含了一些std::ofstream以类似方式向前声明的 iostream 标头。但是 的实际定义std::ofstream<fstream>标题中。



(Note: In the future be sure to provide a Minimal, Complete, Verifiable Exampleinstead of just one function out of your code. You should supply a complete program that demonstrates the problem. This would have been better, for instance:

(注意:将来一定要提供一个最小的、完整的、可验证的示例,而不仅仅是代码中的一个函数。您应该提供一个完整的程序来演示问题。这样会更好,例如:

#include <iostream>

int main() {
    std::ofstream outFile("Log.txt");
}

...also, "Output" is generally seen as one complete word, not two as "OutPut")

...此外,“输出”通常被视为一个完整的词,而不是两个“输出”)