Need help with istream parameters

Write your question here.

For a project I had to write a lexical analyzer that implements the getToken() function. the getToken() is declared in a header file as:

extern Token getToken(istream *br, string& lexeme);

We have to implement the getToken on our own but we cannot change the header that contains getToken. I can't figure out what to pass as a first parameter?
What is *br supposed to take?

Here's a sample of the main function.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(int argc, char* argv[])
{

	string line;
	ifstream infile (argv[1]); 
	istream *br;
       


	if (infile.is_open())
	{
		istream *br;
		br=infile;
                getToken(infile,line);
	}	
	//infile.close(); 



number one error is that you are redefining br. Since br has pointer-to-ifstream type, you should assign the address of inline to it, and not inline itself. You should pass inline's address to the function an not inline itself. Nevertheless, from your code, the br pointer is futile.

Aceix.
Last edited on
Also if the function would have been declared to accept an istream reference you could have passed the ifstream instance directly.

extern Token getToken(istream &br, string& lexeme);
1
2
3
4
5
6
7
8
9
10
int main(int argc, char* argv[])
{

	string line;
	ifstream infile (argv[1]); 

	if (infile.is_open())
	{
                getToken(infile,line);
	}	
Last edited on
Topic archived. No new replies allowed.