简体   繁体   中英

C++ Template Iterator error

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!

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM