Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Template Iterator error

Tags:

c++

I am going over some code i wrote in 2006 as an undergrad. It's a simple genetic algorithm library written in C++ using templates. It use to work in 2006 when i coded it with visual studio, but now when i am trying to run it in xcode i get compile errors.

This function is giving me errors:

friend bool operator==(const TSPGenome<T> & t1, const TSPGenome<T> & t2)
{
    // loop through each interator and check to see if the two genomes have the same values
    if(t1.genome_vec->size() != t2.genome_vec->size())
        return false;
    else
    {
        // iterate through each
        vector<T>::iterator it_t1;
        vector<T>::iterator it_t2;
        it_t1 = t1.genome_vec->begin();
        for(it_t2 = t2.genome_vec->begin();
            it_t2 != t2.genome_vec->end();
            ++it_t2, ++it_t1)
        {
            if(*it_t2 != *it_t1)
                return false;
        }
    }
    // everything seems good
    return true;
}

xcode complains about these two lines not having ; before it_t1 and it_t2.

vector<T>::iterator it_t1;
vector<T>::iterator it_t2;

Is it because the vector type it T?

I declared it in the class as follows:

template <typename T>
class TSPGenome : public Genome
{

Any help would be appreciated.

Thanks!

like image 463
gprime Avatar asked Dec 29 '10 20:12

gprime


1 Answers

Use typename when declaring variables whose class is a member of a template-dependent type:

typename vector<T>::iterator it_t1;

A good description of the need for the typename keyword can be found at A Description of the C++ typename Keyword.

like image 178
Greg Hewgill Avatar answered Oct 22 '22 17:10

Greg Hewgill