Hi All,
I was wondering if anybody can tell me what is wrong with the following
code,
in a .h file I have
std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;
then in the .cpp file
void RmfSystem::step() {
if (itSteps == positions.end()) {
itSteps = positions.begin() ;
} else {
itSteps++ ;
}
}
where step gets called by an animate function in another file. The idea
that I have a vector of vector of positions and I want on each call of
step that the iterator points at another set of positions. Then when the
end of the list (well vector) is reached it loops around to the start.
As it stands my program crashes, as itStep == positions.end() never
seems to be found to be true.
Any ideas what I am doing wrong?
Adam 13 4889
Adam Hartshorne wrote: Hi All,
I was wondering if anybody can tell me what is wrong with the following code,
in a .h file I have
std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator itSteps ;
then in the .cpp file
void RmfSystem::step() {
if (itSteps == positions.end()) {
itSteps = positions.begin() ;
} else {
itSteps++ ;
}
}
where step gets called by an animate function in another file. The idea that I have a vector of vector of positions and I want on each call of step that the iterator points at another set of positions. Then when the end of the list (well vector) is reached it loops around to the start.
As it stands my program crashes, as itStep == positions.end() never seems to be found to be true.
Any ideas what I am doing wrong?
Adam
Among other things, where is the code that initializes
'itSteps'? Somewhere (after 'positions' is filled with
data) you need to do:
itSteps = positions.begin();
A global iterator can cause unexpected side effects
if multiple functions modify the container that
the iterator references. This is a risky design.
Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.
Adam Hartshorne wrote: in a .h file I have
std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator itSteps ;
then in the .cpp file
void RmfSystem::step() {
if (itSteps == positions.end()) {
itSteps = positions.begin() ;
} else {
itSteps++ ;
}
}
I don't see any errors in the above code per se. However, it's generally
*not* a good idea to define global variables in a header file.
My guess is that the error can be found elsewhere, e.g. calling erase.
It /can/ invalidate your iterator.
--
Peter
Larry I Smith wrote: Adam Hartshorne wrote:
Hi All,
I was wondering if anybody can tell me what is wrong with the following code,
in a .h file I have
std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator itSteps ;
then in the .cpp file
void RmfSystem::step() {
if (itSteps == positions.end()) {
itSteps = positions.begin() ;
} else {
itSteps++ ;
}
}
where step gets called by an animate function in another file. The idea that I have a vector of vector of positions and I want on each call of step that the iterator points at another set of positions. Then when the end of the list (well vector) is reached it loops around to the start.
As it stands my program crashes, as itStep == positions.end() never seems to be found to be true.
Any ideas what I am doing wrong?
Adam
Among other things, where is the code that initializes 'itSteps'? Somewhere (after 'positions' is filled with data) you need to do:
itSteps = positions.begin();
A global iterator can cause unexpected side effects if multiple functions modify the container that the iterator references. This is a risky design.
Regards, Larry
Sorry yes I have the following method
void RmfSystem::setUpAnimation() {
itSteps = positions.begin() ;
}
and it works fine, but like I say fails to find match the .end and hence
then loop around.
How should I do this instead?
Adam
Adam Hartshorne wrote: Larry I Smith wrote:
Adam Hartshorne wrote:
Hi All,
I was wondering if anybody can tell me what is wrong with the following code,
in a .h file I have
std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator itSteps ;
then in the .cpp file
void RmfSystem::step() {
if (itSteps == positions.end()) {
itSteps = positions.begin() ;
} else {
itSteps++ ;
}
}
where step gets called by an animate function in another file. The idea that I have a vector of vector of positions and I want on each call of step that the iterator points at another set of positions. Then when the end of the list (well vector) is reached it loops around to the start.
As it stands my program crashes, as itStep == positions.end() never seems to be found to be true.
Any ideas what I am doing wrong?
Adam Among other things, where is the code that initializes 'itSteps'? Somewhere (after 'positions' is filled with data) you need to do:
itSteps = positions.begin();
A global iterator can cause unexpected side effects if multiple functions modify the container that the iterator references. This is a risky design.
Regards, Larry
Sorry yes I have the following method
void RmfSystem::setUpAnimation() {
itSteps = positions.begin() ;
}
and it works fine, but like I say fails to find match the .end and hence then loop around.
How should I do this instead?
Adam
I think I have worked out the problem, but not sure how to solve it. I
create the iterator at the same time as the list is created. However, I
then go on to fill the list up. I need to really create/renew the
iterator when the list has been finished been filled, but not sure how I
can do that, and still have it globally available.
Adam
"Adam Hartshorne" <or********@yahoo.com> wrote in message
news:d7**********@wisteria.csv.warwick.ac.uk... Adam Hartshorne wrote:
Larry I Smith wrote:
Adam Hartshorne wrote:
Hi All,
I was wondering if anybody can tell me what is wrong with the following code,
in a .h file I have
std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator itSteps ;
then in the .cpp file
void RmfSystem::step() {
if (itSteps == positions.end()) {
itSteps = positions.begin() ;
} else {
itSteps++ ;
}
}
where step gets called by an animate function in another file. The idea that I have a vector of vector of positions and I want on each call of step that the iterator points at another set of positions. Then when the end of the list (well vector) is reached it loops around to the start.
As it stands my program crashes, as itStep == positions.end() never seems to be found to be true.
Any ideas what I am doing wrong?
Adam Among other things, where is the code that initializes 'itSteps'? Somewhere (after 'positions' is filled with data) you need to do:
itSteps = positions.begin();
A global iterator can cause unexpected side effects if multiple functions modify the container that the iterator references. This is a risky design.
Regards, Larry
Sorry yes I have the following method
void RmfSystem::setUpAnimation() {
itSteps = positions.begin() ; }
and it works fine, but like I say fails to find match the .end and hence then loop around.
How should I do this instead?
Adam
I think I have worked out the problem, but not sure how to solve it. I create the iterator at the same time as the list is created. However, I then go on to fill the list up. I need to really create/renew the iterator when the list has been finished been filled, but not sure how I can do that, and still have it globally available.
Adam
Just make sure to set the iterator to begin() *after* filling up the list.
Also, reset it if you remove anything from the list. But why not make those
both members of the RmfSystem class? You can then use member functions of
that class to get or set any Site items using the iterator. (At least move
them out of the header, anyway.)
-Howard
Howard wrote: "Adam Hartshorne" <or********@yahoo.com> wrote in message news:d7**********@wisteria.csv.warwick.ac.uk...
Adam Hartshorne wrote:
Larry I Smith wrote:
Adam Hartshorne wrote:
>Hi All, > >I was wondering if anybody can tell me what is wrong with the following >code, > >in a .h file I have > > std::list<std::vector<Site> > positions ; > std::list<std::vector<Site> >::iterator itSteps ; > >then in the .cpp file > >void RmfSystem::step() { > > if (itSteps == positions.end()) { > > itSteps = positions.begin() ; > > } else { > > itSteps++ ; > > } > >} > >where step gets called by an animate function in another file. The idea >that I have a vector of vector of positions and I want on each call of >step that the iterator points at another set of positions. Then when >the >end of the list (well vector) is reached it loops around to the start. > >As it stands my program crashes, as itStep == positions.end() never >seems to be found to be true. > >Any ideas what I am doing wrong? > >Adam Among other things, where is the code that initializes 'itSteps'? Somewhere (after 'positions' is filled with data) you need to do:
itSteps = positions.begin();
A global iterator can cause unexpected side effects if multiple functions modify the container that the iterator references. This is a risky design.
Regards, Larry
Sorry yes I have the following method
void RmfSystem::setUpAnimation() {
itSteps = positions.begin() ; }
and it works fine, but like I say fails to find match the .end and hence then loop around.
How should I do this instead?
Adam
I think I have worked out the problem, but not sure how to solve it. I create the iterator at the same time as the list is created. However, I then go on to fill the list up. I need to really create/renew the iterator when the list has been finished been filled, but not sure how I can do that, and still have it globally available.
Adam
Just make sure to set the iterator to begin() *after* filling up the list. Also, reset it if you remove anything from the list. But why not make those both members of the RmfSystem class? You can then use member functions of that class to get or set any Site items using the iterator. (At least move them out of the header, anyway.)
-Howard
I have just checked, and I do set set the iterator to begin() after
filling up the list.
Also both are memebers of the RmfSystem Class in the header definition.
To achieve what you suggest, I'm not sure how to do that. Also how can
you 'reset' the iterator?
Adam
Adam Hartshorne wrote: Adam Hartshorne wrote:
Larry I Smith wrote:
Adam Hartshorne wrote:
Hi All,
I was wondering if anybody can tell me what is wrong with the following code,
in a .h file I have
std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator itSteps ;
then in the .cpp file
void RmfSystem::step() {
if (itSteps == positions.end()) {
itSteps = positions.begin() ;
} else {
itSteps++ ;
}
}
where step gets called by an animate function in another file. The idea that I have a vector of vector of positions and I want on each call of step that the iterator points at another set of positions. Then when the end of the list (well vector) is reached it loops around to the start.
As it stands my program crashes, as itStep == positions.end() never seems to be found to be true.
Any ideas what I am doing wrong?
Adam Among other things, where is the code that initializes 'itSteps'? Somewhere (after 'positions' is filled with data) you need to do:
itSteps = positions.begin();
A global iterator can cause unexpected side effects if multiple functions modify the container that the iterator references. This is a risky design.
Regards, Larry
Sorry yes I have the following method
void RmfSystem::setUpAnimation() {
itSteps = positions.begin() ; }
and it works fine, but like I say fails to find match the .end and hence then loop around.
How should I do this instead?
Adam
I think I have worked out the problem, but not sure how to solve it. I create the iterator at the same time as the list is created. However, I then go on to fill the list up. I need to really create/renew the iterator when the list has been finished been filled, but not sure how I can do that, and still have it globally available.
Adam
After the container is filled do:
itSteps = positions.begin();
Perhaps you can just call your RmfSystem::setUpAnimation()
method after the container is filled.
The way you're using these globals is very risky.
It is not normally done, for good reasons. Creating
them in a header file (that may be included by multiple
source files) is a bad idea; you may end up with multiple
copies of them in multiple object files. To ensure that
you get just one copy of each global, try this:
in Site.h:
// state that the global 'positions' and 'itSteps' exist
// somewhere
extern std::list<std::vector<Site> > positions ;
extern std::list<std::vector<Site> >::iterator itSteps ;
in Site.cpp:
// create the global 'positions' and 'itSteps'
std::list<std::vector<Site> > positions ;
std::list<std::vector<Site> >::iterator itSteps ;
Any changes to the container by any function may
invalidate the iterator.
I have no idea what you're trying to do, but perhaps
using a class to encapsulate the container , access to it,
and changes to it, would be a better approach.
Start with a good book on OO Design...
Regards,
Larry
--
Anti-spam address, change each 'X' to '.' to reply directly.
Adam Hartshorne <or********@yahoo.com> wrote in
news:d7**********@wisteria.csv.warwick.ac.uk: Adam Hartshorne wrote:
Larry I Smith wrote:
Adam Hartshorne wrote:
Hi All,
I was wondering if anybody can tell me what is wrong with the following code,
in a .h file I have
std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator itSteps ;
then in the .cpp file
void RmfSystem::step() {
if (itSteps == positions.end()) {
itSteps = positions.begin() ;
} else {
itSteps++ ;
}
}
where step gets called by an animate function in another file. The idea that I have a vector of vector of positions and I want on each call of step that the iterator points at another set of positions. Then when the end of the list (well vector) is reached it loops around to the start.
As it stands my program crashes, as itStep == positions.end() never seems to be found to be true.
Any ideas what I am doing wrong?
Adam Among other things, where is the code that initializes 'itSteps'? Somewhere (after 'positions' is filled with data) you need to do:
itSteps = positions.begin();
A global iterator can cause unexpected side effects if multiple functions modify the container that the iterator references. This is a risky design.
Regards, Larry
Sorry yes I have the following method
void RmfSystem::setUpAnimation() {
itSteps = positions.begin() ;
}
and it works fine, but like I say fails to find match the .end and hence then loop around.
How should I do this instead?
Adam
I think I have worked out the problem, but not sure how to solve it. I create the iterator at the same time as the list is created. However, I then go on to fill the list up. I need to really create/renew the iterator when the list has been finished been filled, but not sure how I can do that, and still have it globally available.
Since you're playing with vector<>, you could use an index instead of an
iterator. Just be careful that your vector doesn't get _shorter_ while
you hold an index into the vector. Also watch out for iterator validity
rules. Remember that on insert, all iterators and references into the
vector may be invalidated. So... doing:
std::vector<int> vec;
// assume vec has say 5 items in it already
int index = 2;
int & vecvalue = vec[index];
vec.push_back(6);
vecvalue = 7;
Is "illegal". After the push_back, the reference vecvalue is no longer
valid. However, if you:
std::vector<int> vec;
// assume vec has say 5 items in it already
int index = 2;
int vecvalue = vec[index];
vec.push_back(6);
vec[index] = 7;
Is "correct". After the push_back, we re-index back into the vector, so
it recalculates on the potentially new area of memory that the vector now
occupies...
So, your step() method becomes:
void RmfSystem::step()
{
if (itSteps < positions.size())
{
++itSteps;
}
else
{
itSteps = 0;
}
}
"Adam Hartshorne" <or********@yahoo.com> wrote in message >> >>void RmfSystem::step() { >> >> if (itSteps == positions.end()) { >> >> itSteps = positions.begin() ; >> >> } else { >> >> itSteps++ ; >> >> } >> >>} >>
You haven't shown the context in which this is used, but I think I see the
problem. If I recall correctly, the iterator end() points to one past the
last valid member of the list, *not* to the last member itself. Given that,
the function should be:
++itSteps;
if (itSteps == positions.end())
itSteps = positions.begin();
(I suppose you could pre-increment inside the if statement, but I prefer to
limit actions to one per line, personally.)
-Howard
"Adam Hartshorne" <or********@yahoo.com> skrev i en meddelelse
news:d7**********@wisteria.csv.warwick.ac.uk... Hi All,
I was wondering if anybody can tell me what is wrong with the following code,
in a .h file I have
std::list<std::vector<Site> > positions ; std::list<std::vector<Site> >::iterator itSteps ;
then in the .cpp file
void RmfSystem::step() {
if (itSteps == positions.end()) {
itSteps = positions.begin() ;
} else {
itSteps++ ;
}
*** 2 }
where step gets called by an animate function in another file. The idea that I have a vector of vector of positions and I want on each call of step that the iterator points at another set of positions. Then when the end of the list (well vector) is reached it loops around to the start.
As it stands my program crashes, as itStep == positions.end() never seems to be found to be true.
Any ideas what I am doing wrong?
Adam
Apart from what others say, I must ask you where you "use" your iterator.
You are not allowed to use it in *** 2 indicated above as you might
dereference end().
/Peter
Andre Kostur wrote: Adam Hartshorne <or********@yahoo.com> wrote in news:d7**********@wisteria.csv.warwick.ac.uk:
Adam Hartshorne wrote:
Larry I Smith wrote:
Adam Hartshorne wrote:
>Hi All, > >I was wondering if anybody can tell me what is wrong with the >following code, > >in a .h file I have > > std::list<std::vector<Site> > positions ; > std::list<std::vector<Site> >::iterator itSteps ; > >then in the .cpp file > >void RmfSystem::step() { > > if (itSteps == positions.end()) { > > itSteps = positions.begin() ; > > } else { > > itSteps++ ; > > } > >} > >where step gets called by an animate function in another file. The >idea that I have a vector of vector of positions and I want on each >call of step that the iterator points at another set of positions. >Then when the end of the list (well vector) is reached it loops >around to the start. > >As it stands my program crashes, as itStep == positions.end() never >seems to be found to be true. > >Any ideas what I am doing wrong? > >Adam Among other things, where is the code that initializes 'itSteps'? Somewhere (after 'positions' is filled with data) you need to do:
itSteps = positions.begin();
A global iterator can cause unexpected side effects if multiple functions modify the container that the iterator references. This is a risky design.
Regards, Larry
Sorry yes I have the following method
void RmfSystem::setUpAnimation() {
itSteps = positions.begin() ;
}
and it works fine, but like I say fails to find match the .end and hence then loop around.
How should I do this instead?
Adam
I think I have worked out the problem, but not sure how to solve it. I create the iterator at the same time as the list is created. However, I then go on to fill the list up. I need to really create/renew the iterator when the list has been finished been filled, but not sure how I can do that, and still have it globally available.
Since you're playing with vector<>, you could use an index instead of an iterator. Just be careful that your vector doesn't get _shorter_ while you hold an index into the vector. Also watch out for iterator validity rules. Remember that on insert, all iterators and references into the vector may be invalidated. So... doing:
std::vector<int> vec;
// assume vec has say 5 items in it already
int index = 2;
int & vecvalue = vec[index];
vec.push_back(6);
vecvalue = 7;
Is "illegal". After the push_back, the reference vecvalue is no longer valid. However, if you:
std::vector<int> vec;
// assume vec has say 5 items in it already
int index = 2;
int vecvalue = vec[index];
vec.push_back(6);
vec[index] = 7;
Is "correct". After the push_back, we re-index back into the vector, so it recalculates on the potentially new area of memory that the vector now occupies...
So, your step() method becomes:
void RmfSystem::step() { if (itSteps < positions.size()) { ++itSteps; } else { itSteps = 0; } }
No using a vector, it is a list of vectors. I am iterating through the
list. I have appeared to solve my problem, but replacing
if (itSteps == positions.end()) {
with
if (itSteps == --positions.end()) {
appears to work correctly.
Adam Hartshorne <or********@yahoo.com> wrote in
news:d7**********@wisteria.csv.warwick.ac.uk: No using a vector, it is a list of vectors. I am iterating through the list. I have appeared to solve my problem, but replacing
if (itSteps == positions.end()) {
with
if (itSteps == --positions.end()) {
appears to work correctly.
Undefined behaviour if positions is empty.
"Adam Hartshorne" <or********@yahoo.com> skrev i en meddelelse
news:d7**********@wisteria.csv.warwick.ac.uk... Andre Kostur wrote:
[snip] if (itSteps == positions.end()) {
with
if (itSteps == --positions.end()) {
appears to work correctly.
As Andre pointed out, this is undefined if the list is empty. It is also an
indication that you are using an invalid iterator as i explained in my other
reply.
/Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Grant Edwards |
last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273
Alan Kay said something I really liked, and I think it applies
equally well to Python as well as the languages...
|
by: CoolPint |
last post by:
I am trying to write a generic heapsort (of course as a self-exercise)
with Iterator interface: something like blow....
But I got into trouble finding out the Iterator to the Child node. If...
|
by: Michael Klatt |
last post by:
I am trying to write an iterator for a std::set that allows the
iterator target to be modified. Here is some relvant code:
template <class Set> // Set is an instance of std::set<>
class...
|
by: Lorenzo Castelli |
last post by:
This is an old problem of mine.
Basically I have an abstract base class which represents a generic iterator
over a collection of elements, and various derived classes that implement
the...
|
by: mailforpr |
last post by:
Hi.
Let me introduce an iterator to you, the so-called "Abstract Iterator"
I developed the other day.
I actually have no idea if there's another "Abstract Iterator" out
there, as I have never...
| |
by: T.A. |
last post by:
I understand why it is not safe to inherit from STL containers, but I have
found (in SGI STL documentation) that for example bidirectional_iterator
class can be used to create your own iterator...
|
by: mailforpr |
last post by:
How do I do that?
The thing is, the only information I have about the iterator is the
iterator itself. No container it is belonging to or anything. Like
template<Iteratorvoid...
|
by: David Bilsby |
last post by:
All
Apologies for cross posing this but I am not sure if this is a VC 8 STL
bug or simply an invalid use of the iterator.
I have a PCI card access class which basically abstracts a third party...
|
by: mkborregaard |
last post by:
Hi,
I have the weirdest problem, and I can not see what is going wrong.
I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
|
by: Luis Zarrabeitia |
last post by:
Hi there.
For most use cases I think about, the iterator protocol is more than enough.
However, on a few cases, I've needed some ugly hacks.
Ex 1:
a = iter() # assume you got the iterator...
|
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,...
| |
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |