首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >C++向量为遗传算法初始化代理向量

C++向量为遗传算法初始化代理向量
EN

Stack Overflow用户
提问于 2018-09-11 02:57:49
回答 2查看 118关注 0票数 -4

我有下面的C++代码,但是我得到了一些错误,比如下面(在代码块之后)代理是我在一个单独的文件中创建的一个类

代码语言:javascript
复制
vector<Agent> population;
for (vector<int>::iterator i = population.begin(); i != population.end(); ++i) {
    population.push_back(new Agent(generateDna(targetString.size())));
}

我得到以下错误

2.没有与这些操作数匹配的运算符" !=“--操作数类型为:__gnu_cxx::__normal_iterator>>!= __gnu_cxx::__normal_iterator>>

3.没有重载函数"std::vector<_Tp,_Alloc>::push_back with _Tp=Agent,_Alloc=std::allocator“的实例与参数列表匹配--参数类型为:(Agent *) --对象类型为: std::vector>

我刚接触c++,所以这些东西可能是不言而喻的,但我不知道它们是什么意思。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-11 03:00:04

主要的问题是,你要迭代的集合,你附加在循环中,甚至通过迭代器,它被定义为int而不是Agent。

还要注意使用new关键字。您必须稍后释放该内存。

解决方案:

代码语言:javascript
复制
vector<Agent> population;
vector<Agent> newPopulation;
for (vector<Agent>::iterator i = population.begin(); i != population.end(); ++i) {
    newPopulation.push_back(Agent(generateDna(targetString.size())));
}
票数 -2
EN

Stack Overflow用户

发布于 2018-09-11 03:08:44

您当前的编译问题是,您试图将std::vector<Agent>::iterator存储到std::vector<int>::iterator中。这是两种完全不同的类型。

还有一个问题是,您试图将一个Agent*存储到Agent的向量中。

总之:

代码语言:javascript
复制
std::vector<Agent> population;

//fill your vector.. otherwise loop is useless because size is 0..
auto size = population.size();
for (unsigned int i = 0; i < size; ++i) {
    population.push_back(Agent(generateDna(targetString.size())));
}
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52263883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档