Board index » cppbuilder » Re: Vector of TPanels?

Re: Vector of TPanels?


2005-03-28 10:02:57 PM
cppbuilder31
By using begin() for the offset it compiles but it still does not seem
to insert the panel into the vector? I have a button that when pressed
does this:
{
m_Points.push_back(new TPanel(Image1));
m_Points[m_Points.size()-1]->Parent = Panel1;
m_Points[m_Points.size()-1]->Top = Y+inc;
m_Points[m_Points.size()-1]->Left = X+inc;
m_Points[m_Points.size()-1]->Width = 10;
m_Points[m_Points.size()-1]->Height = 10;
m_Points[m_Points.size()-1]->Name = "Panel_"+inc;
inc+=10;
}
and another when pressed does only this:
{
m_Points.insert(m_Points.begin()+2, new TPanel(Image1));
}
When I run a simple loop to print the panel names to a memo, one under
another all it shows are the names from the first button routine. There
is no blank space representing the panel I just inserted? I expected
something like
Panel_1
Panel_2
Panel_3
Panel_4
Panel_5
But got
Panel_1
Panel_2
Panel_3
Panel_4
Panel_5
with no space, indicating that the new panel was not inserted into the
array? Is my diagnosis correct, that the panel is not being inserted as
it should? I'm so close to finishing this application if only I can get
the panel to insert between two other panels. Thanks for all the help, I
have come a long way in a few days. Cheers,
Rory.
Duane Hebert wrote:
Quote
"Rory Walsh" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...

>Cheers, between your help and the info I have found on the web its
>becoming clearer(albeit slowly!). Following your last post I was able to
>achieve exactly what I wanted to do(thanks), apart from one thing. I
>want to use the 'insert' member function of vector. I tried the following:
>
>m_Points.insert(m_Points.size()-5, new TPanel(Image1));
>
>where "m_Points.size()-5" is the position I want to place the new
>TPanel. I get the error:
>
>[C++ Error] Unit1.cpp(100): E2285 Could not find a match for
>'_STL::vector<TPanel *,_STL::allocator<TPanel *>>::insert(unsigned
>int,undefined)'
>
>The tests I ran with an integer vector worked fine, for example
>
>scores.insert(scores.begin()-5, 540);


In the second case, you're using an iterator. In the first, you're
using an index. Have you tried using an offset from begin() for
your table insert?

From dinkumware.com c++ reference:

vector::insert
iterator insert(iterator where, const Ty& val);
void insert(iterator where, size_type count, const Ty& val);
template<class InIt>
void insert(iterator where, InIt first, InIt last);


 
 

Re:Re: Vector of TPanels?

"Rory Walsh" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...
Quote
By using begin() for the offset it compiles but it still does not seem
to insert the panel into the vector? I have a button that when pressed
does this:

{
m_Points.push_back(new TPanel(Image1));
m_Points[m_Points.size()-1]->Parent = Panel1;
m_Points[m_Points.size()-1]->Top = Y+inc;
m_Points[m_Points.size()-1]->Left = X+inc;
m_Points[m_Points.size()-1]->Width = 10;
m_Points[m_Points.size()-1]->Height = 10;
m_Points[m_Points.size()-1]->Name = "Panel_"+inc;
inc+=10;
}

and another when pressed does only this:
{
m_Points.insert(m_Points.begin()+2, new TPanel(Image1));
}
push_back() adds an item to the end of the vector.
insert() inserts an item where you tell it. Are you sure that
begin() + 2 is valid? You need to debug into this to see
what's going on.
Wouldn't it be simpler to populate the vector initially with
the max number of null pointers that you expect? Then
when adding, use your index like:
m_Points[whatever index] = new TPanel(Image1);
// set all parameters
and when closing:
delete m_Points[whatever index];
m_Points[whatever index] = 0;
 

Re:Re: Vector of TPanels?

I do initialise the vector with a max number of NULL pointers, and
indeed I had been adding panel with the way your mentioned, I just
started using the push_back function to see how it works.
I have tried with
m_Points.insert(m_Points.begin(), new TPanel(Image1));
which definitely exists but no joy there either. I'll keep messing
around and see what I can come up with. The main problem is that I can't
identify the main problem! Cheers,
Rory.
Duane Hebert wrote:
Quote
"Rory Walsh" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...

>By using begin() for the offset it compiles but it still does not seem
>to insert the panel into the vector? I have a button that when pressed
>does this:
>
>{
>m_Points.push_back(new TPanel(Image1));
>m_Points[m_Points.size()-1]->Parent = Panel1;
>m_Points[m_Points.size()-1]->Top = Y+inc;
>m_Points[m_Points.size()-1]->Left = X+inc;
>m_Points[m_Points.size()-1]->Width = 10;
>m_Points[m_Points.size()-1]->Height = 10;
>m_Points[m_Points.size()-1]->Name = "Panel_"+inc;
>inc+=10;
>}
>
>and another when pressed does only this:
>{
>m_Points.insert(m_Points.begin()+2, new TPanel(Image1));
>}


push_back() adds an item to the end of the vector.
insert() inserts an item where you tell it. Are you sure that
begin() + 2 is valid? You need to debug into this to see
what's going on.

Wouldn't it be simpler to populate the vector initially with
the max number of null pointers that you expect? Then
when adding, use your index like:
m_Points[whatever index] = new TPanel(Image1);
// set all parameters

and when closing:
delete m_Points[whatever index];
m_Points[whatever index] = 0;


 

{smallsort}

Re:Re: Vector of TPanels?

"Rory Walsh" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...
Quote
I do initialise the vector with a max number of NULL pointers, and
indeed I had been adding panel with the way your mentioned, I just
started using the push_back function to see how it works.

I have tried with
m_Points.insert(m_Points.begin(), new TPanel(Image1));
which definitely exists but no joy there either. I'll keep messing
around and see what I can come up with. The main problem is that I can't
identify the main problem! Cheers,
OK. It's hard to see what your problem is from here <g>
push_back() appends. insert() at begin() should add it to
the front. What you have to remember is that both of these
will increase the size of the vector. Using them with a presized
vector may be what the confusion is. For example, inserting at
the beginning, your index for the inserted one should be 0 and
the rest should be incremented. It doesn't sound like what you
want.
If you're just messing around, you may want to look at dumping the
index and using only iterators.
At any rate, good luck. Learning how standard containers work
is not a waste of time though books are often better than trial
and error<g>You may also want to look at std::map and std::list.
 

Re:Re: Vector of TPanels?

Hooray! It works, I can't thank you all enough for the help you have
given me in solving this. I hope that someday if someone else asks the
question I can help them. I rewrote the code from scratch with your
advice and it works great now. Now I only have to make some cosmetic
modifications and it should be ready to for release. It'll be open
source so I hope people can learn all about vectors if they need to from
scouring through the source! Thanks again,
Rory.
Duane Hebert wrote:
Quote
"Rory Walsh" < XXXX@XXXXX.COM >wrote in message news: XXXX@XXXXX.COM ...

>By using begin() for the offset it compiles but it still does not seem
>to insert the panel into the vector? I have a button that when pressed
>does this:
>
>{
>m_Points.push_back(new TPanel(Image1));
>m_Points[m_Points.size()-1]->Parent = Panel1;
>m_Points[m_Points.size()-1]->Top = Y+inc;
>m_Points[m_Points.size()-1]->Left = X+inc;
>m_Points[m_Points.size()-1]->Width = 10;
>m_Points[m_Points.size()-1]->Height = 10;
>m_Points[m_Points.size()-1]->Name = "Panel_"+inc;
>inc+=10;
>}
>
>and another when pressed does only this:
>{
>m_Points.insert(m_Points.begin()+2, new TPanel(Image1));
>}


push_back() adds an item to the end of the vector.
insert() inserts an item where you tell it. Are you sure that
begin() + 2 is valid? You need to debug into this to see
what's going on.

Wouldn't it be simpler to populate the vector initially with
the max number of null pointers that you expect? Then
when adding, use your index like:
m_Points[whatever index] = new TPanel(Image1);
// set all parameters

and when closing:
delete m_Points[whatever index];
m_Points[whatever index] = 0;