Using a fstream reference with getline

P

Peter Gordon

Is using getline with a fstream reference covered in the standard?

I have three C++ compilers on my system. Two of them compile
the code below without warnings. The third returns the following
error:
t1.cpp:
Error E2285 t1.cpp 14: Could not find a match for
'std::getline<charT,traits,Allocator>(std::ifstream,std::string)'
in function A::readfun()
*** 1 errors in Compile ***

If the fstream is not a reference, it compiles without complaint.

BTW, yeah, I know, I haven't opened a file etc. I've
tried to skinny the code down to the minimum that
produes the error.

#include <fstream>
#include <string>

class A {
public:
A(std::ifstream& _inH) : inH(_inH) {}
void readfun();
void readfun1();
private:
std::ifstream& inH;
};
void A::readfun() {
std::string buf;
std::getline(inH, buf);
}
int main() {
std::ifstream fin;
class A x(fin);
return 0;
}
 
M

Mike Wahler

Peter Gordon said:
Is using getline with a fstream reference covered in the standard?
Yes.

I have three C++ compilers on my system. Two of them compile
the code below without warnings. The third returns the following
error:
t1.cpp:
Error E2285 t1.cpp 14: Could not find a match for
'std::getline<charT,traits,Allocator>(std::ifstream,std::string)'
in function A::readfun()

'std::getline()'s first parameter is type 'std::ifstream&' (that is,
a reference), not 'std::ifstream' (an object).
*** 1 errors in Compile ***

If the fstream is not a reference, it compiles without complaint.

Again, 'std::getline()'s first parameter is a reference.

Which compiler is giving that error message?
BTW, yeah, I know, I haven't opened a file etc. I've
tried to skinny the code down to the minimum that
produes the error.

That's what we like. :)
#include <fstream>
#include <string>

class A {
public:
A(std::ifstream& _inH) : inH(_inH) {}
void readfun();
void readfun1();
private:
std::ifstream& inH;
};
void A::readfun() {
std::string buf;
std::getline(inH, buf);
}
int main() {
std::ifstream fin;
class A x(fin);

The keyword 'class' is redundant (but harmless) here.
return 0;
}

The code looks OK to me. (But of course the call to 'getline()'
will always fail, since you didn't open the stream).

-Mike
 
P

Peter Gordon

'std::getline()'s first parameter is type 'std::ifstream&' (that is,
a reference), not 'std::ifstream' (an object).


Again, 'std::getline()'s first parameter is a reference.

Which compiler is giving that error message?
Borland Ver 55 which is their free download and is dated
about 2000.

I have a registered copy of CBuilder 4.
The following code compiled and linked on it.

void A::readfun() {
std::string buf;
ifstream inH1;
inH1 = inH;
std::getline(inH1, buf);
}

I didn't try to run it. <g>

So I downloaded the later version and it wouldn't
let me re-asign a reference thus, at least it is an
improvement.
I
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top