473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Array of pointers to a vector

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
Jul 19 '05 #1
6 5754
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


Jul 19 '05 #2
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
Jul 19 '05 #3
JesseChen <je*******@eyou.com> wrote in message news:<bm**********@mail.cn99.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

()
Jul 19 '05 #4


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][i]->Print();

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

return 0;
}
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #5
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 <je*******@eyou.com> wrote in message news:<bm**********@mail.cn99.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

Jul 19 '05 #6


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
kb******@gascad.at
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
10
by: BCC | last post by:
Ive been googling and reading through my books but I haven't figured out a solution (much less an elegant one) to create a multidimensional array with a runtime determined number of dimensions. I...
7
by: Ben | last post by:
Hey everybody, I'm working on a program in c++, and I've come up against a problem that I can't figure out. I don't imagine that it's too difficult to solve, but it has been giving me trouble....
26
by: cdg | last post by:
Could anyone correct any mistakes in this example program. I am just trying to return an array back to "main" to be printed out. And I am not sure how a "pointer to an array" is returned to the...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
8
by: isaac2004 | last post by:
hello, i posted with a topic like this but got no real feedback(prob cuz of lapse in my explanation) so i am trying it again. i am trying to set up a function that brings in a txt file and adds the...
4
by: Edward Jensen | last post by:
Hi, I have the following static arrays of different size in a class: in header: static double w2, x2; static double w3, x3; static double w4, x4; in GaussLegendre.cpp:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.