Visual Studio报出这个奇怪的错误:
不允许不完整的类型
当我尝试创建一个std::ofstream对象时。以下是我在函数内编写的代码。
void OutPutLog()
{
std::ofstream outFile("Log.txt");
}
每当它遇到这个问题,Visual Studio就会抛出那个错误。为什么会发生这种情况?
Visual Studio报出这个奇怪的错误:
不允许不完整的类型
当我尝试创建一个std::ofstream对象时。以下是我在函数内编写的代码。
void OutPutLog()
{
std::ofstream outFile("Log.txt");
}
每当它遇到这个问题,Visual Studio就会抛出那个错误。为什么会发生这种情况?
#include <fstream>
。#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);
}
(注意:将来请务必提供最小完整可验证示例,而不仅仅是您代码中的一个函数。您应该提供一个完整的程序来演示问题。例如,这将更好地说明问题:
#include <iostream>
int main() {
std::ofstream outFile("Log.txt");
}
此外,“Output”通常被视为一个完整的单词,而不是“OutPut”的两个单词。
<fstream>
头文件? - Mgetz