By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,426 Members | 3,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,426 IT Pros & Developers. It's quick & easy.

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

P: n/a
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<BoostBitsetStorag e>, 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<BoostBitsetStorag e>, 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=

Sep 19 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
Sep 19 '06 #2

P: n/a

aaragon schrieb:
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:
std::vector<T>::iterator iter;
typename std::vector<T>::iterator iter;
>
}

~StdVectorStorage()
{
}
};
Sep 19 '06 #3

P: n/a
Pierre Barbier de Reuille wrote:
typedef std::vector<T>::iterator iter;
Are you sure you didn't mean typename std::vector<T>::iterator iter; ?

Sumit.
Sep 19 '06 #4

P: n/a

Sumit RAJAN wrote:
Pierre Barbier de Reuille wrote:
typedef std::vector<T>::iterator iter;

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

Sep 19 '06 #5

P: n/a
Sumit RAJAN a écrit :
Pierre Barbier de Reuille wrote:
>typedef std::vector<T>::iterator iter;

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

Sumit.
Yes, sure!

Pierre
Sep 19 '06 #6

P: n/a
aaragon wrote:
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.

Sep 19 '06 #7

P: n/a

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".

aaragon wrote:
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()
{
}
};
Sep 19 '06 #8

P: n/a
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,
template <classclass StoragePolicy = StdVectorStorage
>
class Population : public StoragePolicy<Individual>
{
public:

gaParameters* gaParams_;

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

};

template
<
typename Individual,
template <classclass StoragePolicy
>
void Population<Individual,StoragePolicy>::initialize(g aParameters* 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<BoostBitsetStoragechrom;
typedef Individual<chrom, HeapStorageind;
typedef Population<ind,StdVectorStoragepop;
....

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,
template <classclass StoragePolicy = StdVectorStorage
>
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.
Gianni Mariani wrote:
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".

aaragon wrote:
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()
{
}
};
Sep 19 '06 #9

P: n/a
"aaragon" <al**************@gmail.comwrites:
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:
>... qualifiers beginning with an underscore are
reserved for the compiler + stdlib
implementation.

Best wishes

Sep 20 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.