简体   繁体   English

C ++模板迭代器错误

[英]C++ Template Iterator error

I am going over some code i wrote in 2006 as an undergrad. 我正在阅读我在2006年写的一些代码作为本科生。 It's a simple genetic algorithm library written in C++ using templates. 这是一个使用模板用C ++编写的简单遗传算法库。 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. 它用于2006年,当我使用visual studio编码时,但现在当我尝试在xcode中运行它时,我得到编译错误。

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 ; xcode抱怨这两行没有; before it_t1 and it_t2. 在it_t1和it_t2之前。

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

Is it because the vector type it T? 是因为它的矢量类型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

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 . 有关typename关键字需求的详细说明,请参阅C ++ typename关键字的描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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