473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using a fstream reference with getline

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::ifstrea m,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;
}

Jul 23 '05 #1
2 4225
"Peter Gordon" <petergo@_deleteme_.netspace.net.au> wrote in message
news:Xn**********************************@203.10.1 10.105...
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::ifstrea m,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

Jul 23 '05 #2
"Mike Wahler" <mk******@mkwahler.net> wrote in news:UKtZd.9167$oO4.3964
@newsread3.news.pas.earthlink.net:
"Peter Gordon" <petergo@_deleteme_.netspace.net.au> wrote in message
news:Xn**********************************@203.10.1 10.105...
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::ifstrea m,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?

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
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christopher Reeve | last post by:
Hi, I wonder if anyone could help me. I am moving accross from C to C++ and am writing a program that reads and writes data into a file. I am aware of the old C ways of doing it but am trying to...
5
by: DellBoy | last post by:
I would like to be able to do something like this: string sFileInfo = "MyFile.tmp"; fstream InfoFile; InfoFile.open(sFileInfo, fstream::app); // >> do some i/o operations << InfoFile.close();...
7
by: Greg | last post by:
I am trying to convert an older console application written in C to a visual application using several Borland VCL components (open and save dialogs). I am declaring input and output stream...
7
by: jccorreu | last post by:
I've got to read info from multiple files that will be given to me. I know the format and what the data is. The thing is each time we run the program we may be using a differnt number of files,...
2
by: manwanirg | last post by:
the function getline is a public member of istream and cin.getline can be used. Since ifstream is publicily derived from istream, getline shall be available in ifstream as well. However,on solaris...
3
by: Wilson | last post by:
hi, one aspect of a program i am creating needs to extract a certain line of text from a simple ".txt" file. However i can only find a way of extract everything present in the file using i/o...
11
by: rory | last post by:
I am reading a binary file and I want to search it for a string. The only problem is that failbit gets set after only a few calls to getline() so it never reaches the end of the file where the...
1
by: barcaroller | last post by:
I was looking at some code that uses fstream: #include <fstream> // // which includes: // istream& getline( char* buffer, streamsize num ); // ifstream fin;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.