istream parameter question

Mar 5, 2015 at 7:57pm
For a class project, my professor wanted us to use

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


The getToken() function is implemented in another cpp file, and used in a main file. When i try to give it an ifstream, or cin as a parameter, i get an error. I don't have much experience with I/O, but isn't istream supposed to recognize both if and iostreams? I saw other examples online and it has the istream parameter as a reference, but the professor doesn't want us changing the header file.
Mar 5, 2015 at 8:03pm
Show the actual code, it's hard for someone to help you without seeing how you're calling the function. My guess is that you're not passing the address of cin, would be be passed as "&cin" and not just cin.
Last edited on Mar 5, 2015 at 8:05pm
Mar 5, 2015 at 8:05pm
Instead of

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

consider

Token getToken(istream &br, string& lexeme);
Mar 5, 2015 at 8:12pm
@LB His professor is probably trying to get them to see the difference between pointers and references, he said the professor didn't want them editing the header.
That being said, I agree it's pointless to use pointers here when references are more syntactically clean and safer. Weird way of teaching.
Last edited on Mar 5, 2015 at 8:13pm
Mar 5, 2015 at 8:14pm
Ah, I missed that the header could not be changed. In that case, you just need to take the address of std::cin.
Topic archived. No new replies allowed.