473,799 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<c harT,traits,All ocator>(std::if stream,std::str ing)'
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(in H, buf);
}
int main() {
std::ifstream fin;
class A x(fin);
return 0;
}

Jul 23 '05 #1
2 4257
"Peter Gordon" <petergo@_delet eme_.netspace.n et.au> wrote in message
news:Xn******** *************** ***********@203 .10.110.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<c harT,traits,All ocator>(std::if stream,std::str ing)'
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(in H, 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******@mkwah ler.net> wrote in news:UKtZd.9167 $oO4.3964
@newsread3.news .pas.earthlink. net:
"Peter Gordon" <petergo@_delet eme_.netspace.n et.au> wrote in message
news:Xn******** *************** ***********@203 .10.110.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<c harT,traits,All ocator>(std::if stream,std::str ing)'
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(in H1, 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
4009
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 use the C++ functions which don't seem to be working propperly. I need to open the file for reading and writing because I want to search for a possition in a file called settings.dat and then write a number after a certain chatacter. However when...
5
4282
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(); return 0; How do I convert the filename string to the const char * that fstream
7
5645
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 variables as follows: ifstream InputFile; ofstream OutputFile; I am initializing them using the filename properties in the "open" and "save" dialogs respectively, with the "._C_str" modifier attached to
7
5562
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, with different file names each time. So i'm writing into the code to ask the user how many files, and what their names are. From each we'll read in 2 lines, then do some math using all of those lines. Then do it again on another set of lines. ...
2
28710
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 ver 2.7, getline is not recognised. e.g ifstream f("a.cpp"); string s; f.getline(s,100); why doesn't complier recognise fstream::getline(string,int) even though istream::getline(string,int) is recognized
3
1934
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 streams. Below is the code i used to extract the whole text, is there any way of extracting a certain line or sentance? string line; ifstream myfile ("example.txt"); if (myfile.is_open()) {
11
13503
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 string is contained. From reading through posts to this list it seems that failbit gets set if there is a format error whilst reading. Is it bad form to reading binary data into a char array? Is this why my function below doesn't work? void...
1
1549
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
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.