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 4920
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::setU pAnimation() {
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::setU pAnimation() {
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********@yah oo.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::setU pAnimation() {
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********@yah oo.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::setU pAnimation() {
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::setU pAnimation() {
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::setU pAnimation()
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********@yah oo.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::setU pAnimation() {
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********@yah oo.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 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 mentioned:
I characterized one way of looking at languages in this
way: a lot of them are either the agglutination of features
or they're a crystallization of style. Languages such as
APL, Lisp, and Smalltalk are what you might call style...
|
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
indexing was used, I could do something like child = hole * 2 + 1; but
since only thing the function accepts are random access Iterators, how
do I calculate the Iterator to the child node?
template <typename Iterator, typename Functor>
void...
|
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 Iterator
{
public :
typedef typename Set::value_type T;
typedef typename Set::iterator SetIterator;
Iterator(Set& container, const SetIterator& it);
|
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 traversing on specific data structures.
For each iterator I want to be able to specify the four possible const
combinations, corresponding to the various void* for
pointers, with the possibility to perform conversions from non-const to
const just like...
|
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 looked for one on the net (I did browse the
boost library though). It doesn't matter right now, anyway.
To put it simply, Abstract Iterator is mainly a wrapper class. It helps
| |
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 classes by inheriting from
it, ie.
class my_bidirectional_iterator : public bidirectional_iterator<double>
{
...
};
|
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 totally_isolated(Iterator& it)
{
//how do I find out if it points to the end node?
}
|
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
library from the higher level classes. This low level class is used to
allocate locked pages of memory before doing a DMA. The third party
library locks memory and returns a pointer to an internal structure,
which we can simply cast to a (void...
|
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 strange. The implementation looks like this (there is a lot of code, but I have only included it all for consistency. The problem is quite small and local):
template<typename T> class Row<T> : public std::vector<T> {};
template<typename...
|
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 from a function and
b = iter() # these two are just examples.
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 we have to send another system
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |