creating a std::vector<T>::iterator ???

A

aaragon

I am trying to create a vector of type T and everything goes fine until
I try to iterate over it. For some reason, the compiler gives me an
error when I declare
std::vector<T>::iterator iter;
Any ideas why is tihs happening? The code is as follows:

template <class T>
struct StdVectorStorage
{
std::vector<T>* _storage;

void create(size_t p,size_t l)
{
_storage = new std::vector<T>(p);

std::vector<T>::iterator iter;

}

~StdVectorStorage()
{
}
};

g++ -c -o main.o main.cxx -pg -O2 -Wall -Wno-sign-compare
gaPolicies.h: In member function 'void
StdVectorStorage<T>::create(size_t, size_t)':
gaPolicies.h:37: error: expected `;' before 'iter'
gaPopulation.h: In member function 'void Population<Individual,
StoragePolicy>::initialize(gaParameters*) [with Individual =
Individual<Chromosome<BoostBitsetStorage>, HeapStorage>, StoragePolicy
= StdVectorStorage]':
gaSimpleGA.h:152: instantiated from 'void gaSimple<Population,
SelectionPolicy>::initialize(int, char**) [with Population =
Population<Individual<Chromosome<BoostBitsetStorage>, HeapStorage>,
StdVectorStorage>, SelectionPolicy = ProportionateSelector]'
main.cxx:16: instantiated from here
gaPopulation.h:36: warning: unused variable 'xsite_'
gaPolicies.h: In member function 'void
StdVectorStorage<T>::create(size_t, size_t) [with T =
Individual<Chromosome<BoostBitsetStorage>, HeapStorage>]':
gaPopulation.h:40: instantiated from 'void Population<Individual,
StoragePolicy>::initialize(gaParameters*) [with Individual =
Individual<Chromosome<BoostBitsetStorage>, HeapStorage>, StoragePolicy
= StdVectorStorage]'
gaSimpleGA.h:152: instantiated from 'void gaSimple<Population,
SelectionPolicy>::initialize(int, char**) [with Population =
Population<Individual<Chromosome<BoostBitsetStorage>, HeapStorage>,
StdVectorStorage>, SelectionPolicy = ProportionateSelector]'
main.cxx:16: instantiated from here
gaPolicies.h:37: error: dependent-name
'std::vector<T,std::allocator<_CharT> >::iterator' is parsed as a
non-type, but instantiation yields a type
gaPolicies.h:37: note: say 'typename
std::vector<T,std::allocator<_CharT> >::iterator' if a type is meant
make: *** [main.o] Error 1=
 
P

Pierre Barbier de Reuille

aaragon a écrit :
I am trying to create a vector of type T and everything goes fine until
I try to iterate over it. For some reason, the compiler gives me an
error when I declare
std::vector<T>::iterator iter;
Any ideas why is tihs happening? The code is as follows:

template <class T>
struct StdVectorStorage
{
std::vector<T>* _storage;

void create(size_t p,size_t l)
{
_storage = new std::vector<T>(p);

std::vector<T>::iterator iter;

}

~StdVectorStorage()
{
}
};

When using a type defined in a template, you have to use the keyword
"typename" so as to avoid any ambiguity:


typedef std::vector<T>::iterator iter;

Pierre
 
M

Marco Wahl

aaragon said:
I am trying to create a vector of type T and everything goes fine until
I try to iterate over it. For some reason, the compiler gives me an
error when I declare
std::vector<T>::iterator iter;
Any ideas why is tihs happening? The code is as follows:

template <class T>
struct StdVectorStorage
{
std::vector<T>* _storage;

void create(size_t p,size_t l)
{
_storage = new std::vector<T>(p);

Maybe the compiler needs a suggestion here:
 
A

aaragon

Sumit said:
Are you sure you didn't mean typename std::vector<T>::iterator iter; ?

Sumit.

It works now! I put

typename std::vector<T>::iterator it_;

Thank you guys for your help.

aa
 
F

F.J.K.

aaragon said:
std::vector<T>* _storage;

Just as an aside. All qualifiers beginning with an underscore are
reserved for the compiler + stdlib implementation. storage_ would be
much better.
 
G

Gianni Mariani

I suspect you got the need to use "typename" when pulling a type out of
a dependant class.

I'd like to make sure though that you know why your using "new".
I am trying to create a vector of type T and everything goes fine until
I try to iterate over it. For some reason, the compiler gives me an
error when I declare
std::vector<T>::iterator iter;
Any ideas why is tihs happening? The code is as follows:

template <class T>
struct StdVectorStorage
{
std::vector<T>* _storage;

void create(size_t p,size_t l)
{

////// Why dynamically create the object ?
_storage = new std::vector<T>(p);

std::vector<T>::iterator iter;

}

~StdVectorStorage()
{

///// Object is not being destoyed - why not ?


Can you use this code ?


template <class T>
struct StdVectorStorage
{
std::vector<T> storage;

void create(size_t p,size_t l)
{
storage = std::vector<T>(p);

}

~StdVectorStorage()
{
}
};
 
A

aaragon

Thanks for your tips. I will be using thisNotation_ for variables from
now on. Ok, this is the thing. I'm trying to write a genetic
algorithm library usign policy based design so I can implement
different behaviors. One of the behaviors was to define a
StoragePolicy. The following code gives the whole idea:

#ifndef _POPULATION_H
#define _POPULATION_H

#include "gaParameters.h"
#include "gaIndividual.h"

using namespace std;

template <class T>
class StoragePolicy
{
void create(size_t,size_t);
};

template <class T>
struct StdVectorStorage
{
typename std::vector<T>::iterator it_;

std::vector<T>* storage_;

void create(size_t p,size_t l)
{
storage_ = new std::vector<T>(p);
for(it_ = storage_->begin(); it_ != storage_->end(); ++it_)
it_->init(l);
}

protected:

~StdVectorStorage()
{
}
};

template
<
class Individual,
class Population : public StoragePolicy<Individual>
{
public:

gaParameters* gaParams_;

// initialize population
void initialize(gaParameters* p);

};

template
<
typename Individual,
void Population<Individual,StoragePolicy>::initialize(gaParameters* p)
{
// assign pointer to parameter list
gaParams_ = p;
unsigned int xsite_; //!< Crossover point


// create population according to the desired policy
this->create(
(size_t)gaParams_->get(popSize),
(size_t)gaParams_->get(lChrom));

}

#endif

Now, since I'm using a storage policy, I thought that it may be nice to
try different data structures to hold the population. One of them of
course is the std::vector so that is the first one I'm using here.
Then, the user will only need to specify the storage used in this
context:

int main(int argc, char *argv[]){

typedef Chromosome<BoostBitsetStorage> chrom;
typedef Individual<chrom, HeapStorage> ind;
typedef Population<ind,StdVectorStorage> pop;
....

There are many things that bother me from this design:
1. I didn't know if it was fine to declare the population size as
template
<
class Pointer,
class Individual,
class Population : public StoragePolicy<Individual>
{
Pointer *pointee_;
....

because then the user needs to know what the Pointer points to (and
this information is supposed to be within the StoragePolicy).
Therefore, I created within the policy class, a pointee_ variable that
points to the actual data structures where the individuals are created.
Is there a better way to accomplish this?

2. Now, If I create another policy parallel to the StoragePolicy, then
I need to know what kind of data structure the StoragePolicy is using
and this is really what is bothering me right now because it is telling
me that this policy cannot be orthogonalized with other policies
(unless I can access the structure using a random access iterator,
thing that I cannot do with my knowledge of C++ at this moment).

Please let me know if you see any improvement on this code.
 
M

Marco Wahl

aaragon said:
Thanks for your tips. I will be using thisNotation_ for variables from
now on.
Wonderful!

... The following code:

#ifndef _POPULATION_H
#define _POPULATION_H

Ahrgh! Again there is a leading underscore! Remember F.K.J.s words:


Best wishes
 

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

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top