gpt4 book ai didi

c++ - 使用 getline 错误读取全局 istream*

转载 作者:行者123 更新时间:2023-11-28 03:08:10 25 4
gpt4 key购买 nike

我正在尝试使用以下代码读取全局 istream*:

/*Global Declaration*/
istream* fp;

/* in main */
ifstream iFile;
if(argc == 2)
//open file code
fp = &file;
else
fp = &cin;
readFile;

/*readFile*/
readFile(){
string line;
while(fp.getline(line))
cout<<line<<endl;
}

我收到以下错误代码:"在 fp 中请求成员 getline,它是非类类型 `std::istream*'谁能告诉我错误是什么,是否有更好的方法来解决这个问题?我确实尝试了 getline(fp, line) 但那里也有更多错误。

最佳答案

您将 fp 声明为指针,但试图将其用作实例。您的 readfile 函数应如下所示:

void readFile()
{
string line;
while(std::getline(*fp, line)) // note the de-referencing of fp
{
cout<<line<<endl;
}
}

(您的代码中还有其他几个语法错误,我假设这些错误只是拼写错误)。

关于c++ - 使用 getline 错误读取全局 istream*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19166233/

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