Like Tree1Likes
  • 1 Post By oogabooga

Trouble storing ifstream object in pair object

This is a discussion on Trouble storing ifstream object in pair object within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to store an ifstream in a pair object, but when I try to assign to pair_name ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Trouble storing ifstream object in pair object

    Hello, I am trying to store an ifstream in a pair object, but when I try to assign to pair_name.second (the ifstream object) an existing ifstream object, it fails with compiler errors hinting that the assignment operator of the ios::base type is private.

    Any ideas what I can do to solve the problem?

    Thanks.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    Use a reference to an ifstream instead.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    Use a reference to an ifstream instead.
    The problem is, the ifstream object I'm trying to assign to pair's object is a local object of a function, and so is the pair, and I'm adding the pair to a global vector. Why does it have to be a reference?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    3,831
    You can't copy C++ library stream objects.

    Use a smart pointer with a null deleter or work on a different design that doesn't need a stream object.

    Soma

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    5,889
    Or, if the stream is guaranteed to exist for at least as long as the pair object, use a std::pair<whatever, ifstream *> and store a pointer to the stream.

    That eliminates the need to copy the actual ifstream object around - which is not allowed, and is the cause of the compilation errors.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by grumpy View Post
    Or, if the stream is guaranteed to exist for at least as long as the pair object, use a std:air<whatever, ifstream *> and store a pointer to the stream.

    That eliminates the need to copy the actual ifstream object around - which is not allowed, and is the cause of the compilation errors.
    Well, a pointer wouldn't work either, because I would be pointing at a local ifstream object in a function which is then destroyed after the function completes its execution, thus making it impossible to use that object in other functions, which is what I hoped to do, which is why I wanted to copy the object.
    So I changed my design, and am no longer trying to hold on to the ifstream objects. I instead merely store the filenames of the files associated with those ifstream objects, along with the content of each file, so I can later open those files again in other functions using completely different ifstream objects, if I need to.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Well, a pointer wouldn't work either, because I would be pointing at a local ifstream object in a function which is then destroyed after the function completes its execution
    But if you used a pointer, you wouldn't point it to a local object, but would instead create a heap object with new.
    Elysia likes this.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    You could use new along with a std::unique_ptr. Remember to use appropriate std::move.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-09-2010, 04:57 PM
  2. Core Dump when closing the ifstream object- why?
    By Krsh in forum C++ Programming
    Replies: 11
    Last Post: 11-20-2008, 05:03 AM
  3. Resetting a ifstream object after reading the whole file
    By Zeeshan in forum C++ Programming
    Replies: 5
    Last Post: 03-31-2008, 08:03 AM
  4. how to pass an ifstream object to a function
    By chintugavali in forum C++ Programming
    Replies: 14
    Last Post: 12-18-2007, 08:58 PM
  5. how do i get the file pointer in a ifstream object?
    By mickey in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2002, 01:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21