The code is:
std::string fname;
std::cin >> fname;
When the code lies in main
function, everything goes well.
But when I put these two lines inside a member function, I get a segmentation fault atruntime.
Can anyone give me some hint on what is going on?
Minimal example:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
class TextQuery {
private:
std::vector<std::string> *lines_of_text;
public:
void retrieve_text();
};
void TextQuery::retrieve_text() {
std::cout<<"Please input file name:\n";
std::string fname;
std::cin >> fname;
std::ifstream fcontent(fname.c_str(), std::ios::in);
std::string text_line;
while(getline(fcontent, text_line, '\n')) {
lines_of_text->push_back(text_line);
}
}
int main() {
TextQuery tq;
tq.retrieve_text();
return 0;
}
I am using g++ 4.2.1 on MacOS.