gpt4 book ai didi

C++ cin 段错误 11

转载 作者:搜寻专家 更新时间:2023-10-31 00:27:04 25 4
gpt4 key购买 nike

<分区>

代码是:

std::string fname;
std::cin >> fname;

当代码位于main 函数时,一切顺利。但是当我将这两行放在一个成员函数中时,我在运行时遇到了段错误。

有人能给我一些提示吗?

最小的例子:

#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;
}

我在 MacOS 上使用 g++ 4.2.1。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com