C++模板迭代器错误

gpr*_*ime 7 c++

我正在阅读我在2006年写的一些代码作为本科生.这是一个使用模板用C++编写的简单遗传算法库.它用于2006年,当我使用visual studio编码时,但现在当我尝试在xcode中运行它时,我得到编译错误.

这个函数给了我错误:

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;
}
Run Code Online (Sandbox Code Playgroud)

xcode抱怨这两行没有; 在it_t1和it_t2之前.

vector<T>::iterator it_t1;
vector<T>::iterator it_t2;
Run Code Online (Sandbox Code Playgroud)

是因为它的矢量类型T?

我在课堂上宣布如下:

template <typename T>
class TSPGenome : public Genome
{
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

谢谢!

Gre*_*ill 17

typename在声明其类是模板相关类型成员的变量时使用:

typename vector<T>::iterator it_t1;
Run Code Online (Sandbox Code Playgroud)

typename可以在C++ typename关键字的描述中找到关键字需求的良好描述.