开发者

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_t开发者_如何学JAVA1 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜