Sorry if this is pretty noobish, but I'm pretty new to C++. I'm trying to open a file and read it using ifstream
:
很抱歉,如果这很无聊的话,但是我对c++很陌生。我正在尝试打开一个文件并使用ifstream读取它:
vector<string> load_f(string file) {
vector<string> text;
ifstream ifs(file);
string buffer, str_line;
int brackets = 0;
str_line = "";
while ( getline(ifs, buffer) ) {
buffer = Trim( buffer );
size_t s = buffer.find_first_of("()");
if (s == string::npos) str_line += "" + buffer;
else {
while ( s != string::npos ) {
str_line += "" + buffer.substr(0, s + 1);
brackets += (buffer[s] == '(' ? 1 : -1);
if ( brackets == 0 ) {
text.push_back( str_line );
str_line = "";
}
buffer = buffer.substr(s + 1);
s = buffer.find_first_of("()");
}
}
}
return text;
}
However, I'm getting the following error I'm not quite sure how to fix:
但是,我得到了以下错误,我不太确定如何修复:
variable 'std::ifstream ifs' has initializer but incomplete type
Answers very appreciated. Note that I never forgot to #include <fstream>
, since many have gotten the error due to just forgetting to include the header.
答案非常感激。请注意,我从来没有忘记#include
EDIT:
编辑:
Turns out that I actually did forget to include fstream
, but I had forgotten due to moving the function to another file.
实际上,我确实忘记了包含fstream,但是由于将函数移动到另一个文件,我已经忘记了。
1 个解决方案
#1
54
This seems to be answered - #include <fstream>
.
这似乎是答案- #include
The message means :-
消息的意思是:-
incomplete type
- the class has not been defined with a full class. The compiler has seen statements such as class ifstream;
which allow it to understand that a class exists, but does not know how much memory the class takes up.
不完整类型——类没有被定义为一个完整的类。编译器已经看到了诸如类ifstream之类的语句;这使它能够理解类的存在,但不知道该类占用了多少内存。
The forward declaration allows the compiler to make more sense of :-
前向声明允许编译器更有意义:-
void BindInput( ifstream & inputChannel );
It understands the class exists, and can send pointers and references through code without being able to create the class, see any data within the class, or call any methods of the class.
它理解类的存在,并且可以通过代码发送指针和引用,而无需创建类、查看类中的任何数据或调用类的任何方法。
The has initializer
seems a bit extraneous, but is saying that the incomplete object is being created.
has初始化似乎有点无关紧要,但它表示正在创建不完整的对象。
#1
54
This seems to be answered - #include <fstream>
.
这似乎是答案- #include
The message means :-
消息的意思是:-
incomplete type
- the class has not been defined with a full class. The compiler has seen statements such as class ifstream;
which allow it to understand that a class exists, but does not know how much memory the class takes up.
不完整类型——类没有被定义为一个完整的类。编译器已经看到了诸如类ifstream之类的语句;这使它能够理解类的存在,但不知道该类占用了多少内存。
The forward declaration allows the compiler to make more sense of :-
前向声明允许编译器更有意义:-
void BindInput( ifstream & inputChannel );
It understands the class exists, and can send pointers and references through code without being able to create the class, see any data within the class, or call any methods of the class.
它理解类的存在,并且可以通过代码发送指针和引用,而无需创建类、查看类中的任何数据或调用类的任何方法。
The has initializer
seems a bit extraneous, but is saying that the incomplete object is being created.
has初始化似乎有点无关紧要,但它表示正在创建不完整的对象。