C++ &引用;不允许使用不完整的类型“;创建std::of流对象时
Visual Studio抛出此奇怪错误: 不允许使用不完整的类型 当我尝试创建std::ofstream对象时。下面是我在函数中编写的代码C++ &引用;不允许使用不完整的类型“;创建std::of流对象时,c++,visual-studio-2013,fstream,ofstream,C++,Visual Studio 2013,Fstream,Ofstream,Visual Studio抛出此奇怪错误: 不允许使用不完整的类型 当我尝试创建std::ofstream对象时。下面是我在函数中编写的代码 void OutPutLog() { std::ofstream outFile("Log.txt"); } 每当遇到此问题时,VisualStudio都会抛出该错误。为什么会发生这种情况?正如@Mgetz所说,您可能忘记了#包含 您没有得到未声明错误,而此不允许的不完整类型错误的原因与存在已定义但尚未完全定义的类型时发生的情况有关 看看这个例
void OutPutLog()
{
std::ofstream outFile("Log.txt");
}
每当遇到此问题时,VisualStudio都会抛出该错误。为什么会发生这种情况?正如@Mgetz所说,您可能忘记了
#包含
您没有得到未声明
错误,而此不允许的不完整类型
错误的原因与存在已定义但尚未完全定义的类型时发生的情况有关
看看这个例子:
#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);
}
…此外,“输出”通常被视为一个完整的单词,而不是两个完整的单词。”您是否包括了
?包括fstream解决了我的问题。谢谢
#include <iostream>
int main() {
std::ofstream outFile("Log.txt");
}