0

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.

4

1 回答 1

2

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

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

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

于 2013-10-03T18:15:57.803 回答