Array of pointers to a vector

P

Puvendran

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
 
J

Jonas Mellin

Puvendran said:
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?
 
J

JesseChen

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 said:
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
 
P

Puvendran

JesseChen said:
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 said:
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 said:
::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 said:
::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 said:
::push_back
()
 
K

Karl Heinz Buchegger

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;
}
 
P

Puvendran

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 said:
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 said:
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
 
K

Karl Heinz Buchegger

Puvendran said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top