vector of ostringstream

Discussion in 'C++' started by Alex Vinokur, Jul 10, 2003.

  1. Alex Vinokur

    Alex Vinokur Guest

    Is it possible to use vector<ostringstream> ?

    Here is what I have got.

    ===========================================
    Windows 2000
    CYGWIN_NT-5.0 1.3.22(0.78/3/2)
    GNU gcc version 3.2 20020927 (prerelease)
    ===========================================

    // File t.cpp
    --------- C++ code : BEGIN ---------
    #include <vector>
    #include <sstream>
    #include <iostream>
    using namespace std;

    int main ()
    {
    vector<ostringstream> oss;

    oss.push_back (ostringstream()); // Line#10
    oss.front() << "ABCD" << endl;

    cout << oss.front().str();

    return 0;
    }
    --------- C++ code : END -----------


    --------- Compilation : BEGIN ---------

    $ g++ t.cpp

    /usr/include/c++/3.2/bits/ios_base.h: In copy constructor `std::basic_ios<char,
    std::char_traits<char> >::basic_ios(const std::basic_ios<char,
    std::char_traits<char> >&)':
    /usr/include/c++/3.2/bits/stl_construct.h:78: instantiated from `void std::_Construct(_T1*, const _T2&) [with _T1 =
    std::eek:stringstream, _T2 = std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >]'
    /usr/include/c++/3.2/bits/stl_vector.h:492: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp =
    std::eek:stringstream, _Alloc = std::allocator<std::eek:stringstream>]'
    t.cpp:10: instantiated from here
    /usr/include/c++/3.2/bits/ios_base.h:421: `std::ios_base::ios_base(const
    std::ios_base&)' is private
    /usr/include/c++/3.2/bits/stl_construct.h:78: within this context
    /usr/include/c++/3.2/streambuf: In copy constructor `std::basic_stringbuf<char,
    std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const
    std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char>
    >&)':

    /usr/include/c++/3.2/streambuf:486: `std::basic_streambuf<_CharT,
    _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&)
    [with _CharT = char, _Traits = std::char_traits<char>]' is private
    /usr/include/c++/3.2/bits/stl_construct.h:78: within this context
    /usr/include/c++/3.2/bits/ios_base.h: In member function `std::basic_ios<char,
    std::char_traits<char> >& std::basic_ios<char, std::char_traits<char>
    >::eek:perator=(const std::basic_ios<char, std::char_traits<char> >&)':

    /usr/include/c++/3.2/bits/stl_vector.h:893: instantiated from `void std::vector<_Tp,
    _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = std::eek:stringstream,
    _Alloc = std::allocator<std::eek:stringstream>]'
    /usr/include/c++/3.2/bits/stl_vector.h:496: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp =
    std::eek:stringstream, _Alloc = std::allocator<std::eek:stringstream>]'
    t.cpp:10: instantiated from here
    /usr/include/c++/3.2/bits/ios_base.h:424: `std::ios_base&
    std::ios_base::eek:perator=(const std::ios_base&)' is private
    /usr/include/c++/3.2/bits/stl_vector.h:893: within this context
    /usr/include/c++/3.2/streambuf: In member function `std::basic_stringbuf<char,
    std::char_traits<char>, std::allocator<char> >& std::basic_stringbuf<char,
    std::char_traits<char>, std::allocator<char> >::eek:perator=(const
    std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char>
    >&)':

    /usr/include/c++/3.2/streambuf:489: `std::basic_streambuf<_CharT, _Traits>&
    std::basic_streambuf<_CharT, _Traits>::eek:perator=(const
    std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits =
    std::char_traits<char>]' is private
    /usr/include/c++/3.2/bits/stl_vector.h:893: within this context

    --------- Compilation : END -----------

    --
    ==========================================
    Alex Vinokur
    mailto:
    http://www.simtel.net/pub/oth/19088.html
    http://sourceforge.net/users/alexvn
    ==========================================
    Alex Vinokur, Jul 10, 2003
    #1
    1. Advertising

  2. Alex Vinokur wrote:
    > Is it possible to use vector<ostringstream> ?
    >
    > Here is what I have got.
    >
    > ===========================================
    > Windows 2000
    > CYGWIN_NT-5.0 1.3.22(0.78/3/2)
    > GNU gcc version 3.2 20020927 (prerelease)
    > ===========================================
    >
    > // File t.cpp
    > --------- C++ code : BEGIN ---------
    > #include <vector>
    > #include <sstream>
    > #include <iostream>
    > using namespace std;
    >
    > int main ()
    > {
    > vector<ostringstream> oss;
    >
    > oss.push_back (ostringstream()); // Line#10
    > oss.front() << "ABCD" << endl;
    >
    > cout << oss.front().str();
    >
    > return 0;
    > }
    > --------- C++ code : END -----------
    >


    This is probably not what you truly want to do.

    An ostringstream is probably not assignable, meaning you can't "push" them.

    The code below blows up as well.

    #include <sstream>
    #include <iostream>
    using namespace std;

    int main ()
    {

    ostringstream a;
    ostringstream b;

    a = b;

    return 0;
    }

    vector objects need to be assignable so you're going to have a hard time
    with vector<ostringstream>.

    You could create :

    vector<ostringstream *>

    But then you need to know how to delete them. One suggestion is to
    create a class like:

    class foo
    : public ostringstream,
    public lifetime_manager
    {

    };


    class foo_pointer
    {

    foo * m_foo;

    public:

    operator ostringstream &()
    {
    return * m_foo;
    }

    .... all other reference counting smart pointer things like
    constructors, destructors and assignment operators ... I can give you a
    template for these ...

    };


    Now you can write

    vector<foo_pointer> oss;

    oss.push_back (new foo());

    oss.front() << "ABCD" << endl;


    I know it's a bit terse, but depending on what you truly trying to do,
    you might have a more appropriate alternative.

    G
    Gianni Mariani, Jul 10, 2003
    #2
    1. Advertising

  3. "Alex Vinokur" <> writes:

    > Is it possible to use vector<ostringstream> ?


    No. Vector element types have to be copyable, which stream types aren't.


    > /usr/include/c++/3.2/bits/ios_base.h:421: `std::ios_base::ios_base(const
    > std::ios_base&)' is private


    This is how the "noncopyability" is implemented.
    Thomas Maeder, Jul 10, 2003
    #3
  4. Alex Vinokur

    Guy Harrison Guest

    Alex Vinokur wrote:

    > Is it possible to use vector<ostringstream> ?


    Nope, not like that. Never tried it myself until now but those errors are
    basically telling you its impossible.

    > Here is what I have got.
    >
    > ===========================================
    > Windows 2000
    > CYGWIN_NT-5.0 1.3.22(0.78/3/2)
    > GNU gcc version 3.2 20020927 (prerelease)
    > ===========================================
    >
    > // File t.cpp
    > --------- C++ code : BEGIN ---------
    > #include <vector>
    > #include <sstream>
    > #include <iostream>
    > using namespace std;
    >
    > int main ()
    > {
    > vector<ostringstream> oss;
    >
    > oss.push_back (ostringstream()); // Line#10
    > oss.front() << "ABCD" << endl;
    >
    > cout << oss.front().str();
    >
    > return 0;
    > }


    vector<string> methinks!


    --
    Guy Harrison
    Guy Harrison, Jul 11, 2003
    #4
  5. Alex Vinokur

    llewelly Guest

    "Alex Vinokur" <> writes:

    > Is it possible to use vector<ostringstream> ?


    No. ostringstream is not copyable.
    llewelly, Jul 14, 2003
    #5
    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. Chris
    Replies:
    3
    Views:
    2,007
    Chris
    Feb 17, 2004
  2. pmatos
    Replies:
    6
    Views:
    23,727
  3. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    543
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    347
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page