Array of pointers to a vector

Discussion in 'C++' started by Puvendran, Oct 19, 2003.

  1. Puvendran

    Puvendran Guest

    Hi,

    I am trying to create an array of pointers to a vector which holds
    values of a user defined class. The application involved needs
    multiple arrays to point the same vector ie

    class message
    {
    public:
    string from;

    };

    void main()
    {
    vector<message>* Arr1[10];
    vector<message>* Arr2[10];

    message m1;

    m1.from = "john";
    Arr1[0].push_back(&m1);
    )

    I get the following compilation error due to the last line

    test1.cc:45: request for member `push_back' in `n[0]', which is of
    non-aggregate
    type `vector<message,allocator<message> > *'


    What I am trying to achieve in my application is for objects of type
    message to be shared,via pointers, thereby saving memory, rather than
    each array having identical copies of the same object.

    So, what I need is

    1. To be able to add message object to Arr1
    2. Subset of the messages in Arr1 to be referenced by Arr2
    3. Display the contents of Arr1/Arr2.

    Thanking you in advance

    Puvendran
     
    Puvendran, Oct 19, 2003
    #1
    1. Advertising

  2. Puvendran

    Jonas Mellin Guest

    Puvendran wrote:
    > Hi,
    >
    > I am trying to create an array of pointers to a vector which holds
    > values of a user defined class. The application involved needs
    > multiple arrays to point the same vector ie
    >
    > class message
    > {
    > public:
    > string from;
    >
    > };
    >
    > void main()
    > {
    > vector<message>* Arr1[10];
    > vector<message>* Arr2[10];
    >
    > message m1;
    >
    > m1.from = "john";
    > Arr1[0].push_back(&m1);
    > )
    >
    > I get the following compilation error due to the last line
    >
    > test1.cc:45: request for member `push_back' in `n[0]', which is of
    > non-aggregate
    > type `vector<message,allocator<message> > *'


    Should it not be "Arr1[0]->push_back(&m1);"?


    >
    >
    > What I am trying to achieve in my application is for objects of type
    > message to be shared,via pointers, thereby saving memory, rather than
    > each array having identical copies of the same object.


    While I am at it, I recommend shared pointers instead of pure pointer.
    Avoids a lot of hassle together with templates while scaling the same
    way as pure pointer application as well as not adding an overly large
    overhead to your application compared to using pure pointers.

    >
    > So, what I need is
    >
    > 1. To be able to add message object to Arr1
    > 2. Subset of the messages in Arr1 to be referenced by Arr2
    > 3. Display the contents of Arr1/Arr2.


    So, arr1 is a queue of messages and arr2 should be a subset of arr1
    queue. Is that what you want? Why do you have an array of such queues?
    Do you want to handle multiple queues?
    >
    > Thanking you in advance
    >
    > Puvendran
     
    Jonas Mellin, Oct 19, 2003
    #2
    1. Advertising

  3. Puvendran

    JesseChen Guest

    Hi,

    I have serveral doubts here:

    > class message
    > {
    > public:
    > string from;
    >
    > };
    >
    > void main()
    > {
    > vector<message>* Arr1[10];
    > vector<message>* Arr2[10];


    Why not use vector<message*> Array1[10]; here?

    > message m1;
    >
    > m1.from = "john";
    > Arr1[0].push_back(&m1);
    > )


    If as you coded ,why not be Array[0]->push_back(&m1); ?

    I think once you get or duplicate the pointer to an object, you can handle
    the object.

    Jesse
     
    JesseChen, Oct 19, 2003
    #3
  4. Puvendran

    Puvendran Guest

    JesseChen <> wrote in message news:<bmttea$6va$99.com>...
    > Hi,
    >
    > I have serveral doubts here:
    >
    > > class message
    > > {
    > > public:
    > > string from;
    > >
    > > };
    > >
    > > void main()
    > > {
    > > vector<message>* Arr1[10];
    > > vector<message>* Arr2[10];

    >
    > Why not use vector<message*> Array1[10]; here?
    >
    > > message m1;
    > >
    > > m1.from = "john";
    > > Arr1[0].push_back(&m1);
    > > )

    >
    > If as you coded ,why not be Array[0]->push_back(&m1); ?
    >
    > I think once you get or duplicate the pointer to an object, you can handle
    > the object.
    >
    > Jesse


    Basically the program reads a file containig mail messages. You should
    be able to specify different views on the contents eg by who sent the
    mail, subject matter etc. So, to minimise memory impact I did not want
    multiple copies of these , sometimes large , mail messages. Thus the
    idea of a vector of pointers.
    Reason for vector was that different mailboxes could have different
    number of messages so I needed the ability (in effect) to resize the
    array.

    I tried the suggestion but got the following error.

    test1.cc: In function `int main(...)':
    test1.cc:45: no matching function for call to
    `vector<message,allocator<message>
    >::push_back (message *)'

    /gnu/usr/lib/gcc-lib/sparc-sun-solaris2.6/2.95.2/../../../../include/g++-3/stl_v
    ector.h:319: candidates are: void vector<message,allocator<message>
    >::push_back

    (const message &)
    /gnu/usr/lib/gcc-lib/sparc-sun-solaris2.6/2.95.2/../../../../include/g++-3/stl_v
    ector.h:327: void vector<message,allocator<message>
    >::push_back

    ()
     
    Puvendran, Oct 20, 2003
    #4
  5. Puvendran wrote:
    >


    Does the following help ?


    #include <iostream>
    #include <string>
    #include <list>
    #include <vector>
    #include <algorithm>

    using namespace std;

    class Message
    {
    public:
    Message( std::string From, std::string Subject, std::string Body )
    : m_From( From ), m_Subject( Subject ), m_Body( Body )
    {}

    void Print()
    { cout << "***************************\n"
    << "From : " << m_From << "\n"
    << "Subject : " << m_Subject << "\n"
    << "Body : " << m_Body << "\n";
    }

    friend bool SortByFrom( const Message* rhs, const Message* lhs );
    friend bool SortBySubject( const Message* rhs, const Message* lhs );

    protected:
    std::string m_From;
    std::string m_Subject;
    std::string m_Body;
    };

    bool SortByFrom( const Message* lhs, const Message* rhs )
    {
    return lhs->m_From < rhs->m_From;
    }

    bool SortBySubject( const Message* lhs, const Message* rhs )
    {
    return lhs->m_Subject < rhs->m_Subject;
    }

    int main()
    {
    list< Message > TheMessages;
    vector< Message* > Sorted[2];

    //
    // strore the messages somewhere
    //
    TheMessages.push_back( Message( "Kalle", "Your Problem", "This should do it" ) );
    TheMessages.push_back( Message( "Tom", "Time?", "Hi. Do you have time?" ) );
    TheMessages.push_back( Message( "Agnes", "[Re]Time?", "No. I don't?" ) );
    TheMessages.push_back( Message( "Bart", "Universe", "42" ) );

    //
    // fetch the pointers from the original messages
    // and store them in the vectors. Note: Since I have
    // stored the messages in a list rather then in a vector
    // the pointers will remain valid, even if the list gets
    // larger
    for( list<Message>::iterator il = TheMessages.begin();
    il != TheMessages.end();
    il++ ) {
    Sorted[0].push_back( &(*il) );
    Sorted[1].push_back( &(*il) );
    }

    // now sort the arrays holding the pointers
    std::sort( Sorted[0].begin(), Sorted[0].end(), SortByFrom );
    std::sort( Sorted[1].begin(), Sorted[1].end(), SortBySubject );

    cout << "\nSorted by From field\n";
    for( int i = 0; i < Sorted[0].size(); ++i )
    Sorted[0]->Print();

    cout << "\nSorted by Subject field\n";
    for( i = 0; i < Sorted[1].size(); ++i )
    Sorted[1]->Print();

    return 0;
    }


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 20, 2003
    #5
  6. Puvendran

    Puvendran Guest

    Thanks.

    Yes a vector of pointers is the way to go

    So, what I have is

    class message
    {
    public:
    string from;

    };

    void
    message::add(string s)
    {
    from = s;
    }

    message* m1;
    vector<message *> m;

    m.resize(10);
    m1=new message();

    (*m1).add("Hello");
    m[0]=m1;

    This compiles but when I call the add method on pointer object m1, as
    soon as it tries to access data member from in that object I get a
    segmentation violation. I am able to do anything else within the
    pointer object. Sorry I am a newbie and am struggling.



    JesseChen <> wrote in message news:<bmttea$6va$99.com>...
    > Hi,
    >
    > I have serveral doubts here:
    >
    > > class message
    > > {
    > > public:
    > > string from;
    > >
    > > };
    > >
    > > void main()
    > > {
    > > vector<message>* Arr1[10];
    > > vector<message>* Arr2[10];

    >
    > Why not use vector<message*> Array1[10]; here?
    >
    > > message m1;
    > >
    > > m1.from = "john";
    > > Arr1[0].push_back(&m1);
    > > )

    >
    > If as you coded ,why not be Array[0]->push_back(&m1); ?
    >
    > I think once you get or duplicate the pointer to an object, you can handle
    > the object.
    >
    > Jesse
     
    Puvendran, Oct 22, 2003
    #6
  7. Puvendran wrote:
    >
    > Thanks.
    >
    > Yes a vector of pointers is the way to go
    >
    > So, what I have is
    >
    > class message
    > {
    > public:
    > string from;
    >
    > };
    >
    > void
    > message::add(string s)
    > {
    > from = s;
    > }
    >
    > message* m1;
    > vector<message *> m;
    >
    > m.resize(10);
    > m1=new message();
    >
    > (*m1).add("Hello");
    > m[0]=m1;
    >
    > This compiles but when I call the add method on pointer object m1, as
    > soon as it tries to access data member from in that object I get a
    > segmentation violation. I am able to do anything else within the
    > pointer object. Sorry I am a newbie and am struggling.


    .... then it is absolutely important that you post a complete
    compileable program not only fragments. The problem you
    experience may be located somewhere else.

    #include <vector>
    #include <string>
    using namespace std;

    class message
    {
    public:
    void add( const string& s )

    protected:
    string from;
    };

    void message::add( const string& s )
    {
    from = s;
    }

    int main()
    {
    message* m1;
    vector< message *> m;

    m.resize( 10 );

    m1 = new message;
    m1->add( "Hello" );

    m[0] = m1;
    }

    Shouldn't give you this problem. There is still the problem of
    leaking memory, but definitely not segmentation violation.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 22, 2003
    #7
    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. pmatos
    Replies:
    6
    Views:
    23,821
  2. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    567
    James Kanze
    Sep 4, 2007
  4. zl2k
    Replies:
    27
    Views:
    1,588
    Francesco S. Carta
    Sep 7, 2010
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    682
Loading...

Share This Page