gpt4 book ai didi

C++ 模板迭代器错误

转载 作者:可可西里 更新时间:2023-11-01 18:15:21 25 4
gpt4 key购买 nike

我正在复习我在 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;
}

xcode 提示这两行没有 ;在 it_t1 和 it_t2 之前。

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

是不是因为vector类型是T?

我在类中声明如下:

template <typename T>
class TSPGenome : public Genome
{

如有任何帮助,我们将不胜感激。

谢谢!

最佳答案

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

typename vector<T>::iterator it_t1;

可以在 A Description of the C++ typename Keyword 找到对 typename 关键字需求的很好描述。 .

关于C++ 模板迭代器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4557723/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com