How to pass reference of ifstream to function that expects istream

Jan 8, 2021 at 4:59pm
I have a Lexer class, and one of its members is an istream reference. I chose to make it an istream in order to support any kind of input streams that inherit from it - standard input, files, etc..
So Lexer's constructor takes an istream& and initializes the member to that:
1
2
Lexer::Lexer(std::istream& _codeStream) :
codeStream(_codeStream) {}

In my main function, I create a new ifstream object, and I want to pass its reference to Lexer's constructor, but I can't seem to get it to work. How would I go about doing this?
Jan 8, 2021 at 5:06pm
Just pass it. An ifstream is an istream. There shouldn't be a problem.
Jan 8, 2021 at 5:27pm
Ah, you're right. It seems that Visual Studio was just highlighting the first argument as an error, but after building it there was another separate problem entirely.
Topic archived. No new replies allowed.