Using a fstream reference with getline

Discussion in 'C++' started by Peter Gordon, Mar 14, 2005.

  1. Peter Gordon

    Peter Gordon Guest

    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;
    }
     
    Peter Gordon, Mar 14, 2005
    #1
    1. Advertising

  2. Peter Gordon

    Mike Wahler Guest

    "Peter Gordon" <petergo@_deleteme_.netspace.net.au> wrote in message
    news:Xns961A58E1FB631petergonetspacenetau@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<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
     
    Mike Wahler, Mar 15, 2005
    #2
    1. Advertising

  3. Peter Gordon

    Peter Gordon Guest

    "Mike Wahler" <> wrote in news:UKtZd.9167$oO4.3964
    @newsread3.news.pas.earthlink.net:

    > "Peter Gordon" <petergo@_deleteme_.netspace.net.au> wrote in message
    > news:Xns961A58E1FB631petergonetspacenetau@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<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?
    >

    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
     
    Peter Gordon, Mar 15, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fred H
    Replies:
    6
    Views:
    7,416
    Kevin Goodsell
    Jan 23, 2004
  2. Armando
    Replies:
    6
    Views:
    763
    Armando
    Jan 29, 2004
  3. shyam
    Replies:
    3
    Views:
    638
    shyam
    Jan 15, 2007
  4. rory
    Replies:
    15
    Views:
    3,801
    James Kanze
    Jan 25, 2008
  5. Disc Magnet
    Replies:
    3
    Views:
    1,202
    James Kanze
    Mar 5, 2010
Loading...

Share This Page