I'm trying to read a global istream* using the following code:
/*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;
}
I'm getting the following error code: "request for member getline
in fp
, which is of non-class type `std::istream*' Could anyone tell me what the error is, and if there's a better way to go about it? I did try getline(fp, line) but had more errors there too.
You are declaring fp
as a pointer, but trying to use it as an instance. Your readfile function should look like this:
void readFile()
{
string line;
while(std::getline(*fp, line)) // note the de-referencing of fp
{
cout<<line<<endl;
}
}
(You also have several other syntax errors in your code that I'm assuming are just typos).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.