我正在尝试用c ++制作兔子育种计划。我在这一行的期间内一直收到错误......
RabbitsM.push_back(Rabbit());
我需要能够确定兔子是雌性还是雄性,它们的颜色是什么。所以我想的是我制作了两个矢量(男性和女性),我想在矢量中有4个插槽,以获得后代的颜色(棕色,白色,黑色和斑点)。我一直得到的错误是......
Severity Code Description Project File Line
Error (active) no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=float, _Alloc=std::allocator<float>]" matches the argument list
这是我的源代码......
class Rabbit {
int main()
{
for (int i = 0; i < 20; i++)
{
int rabbits = 5;
vector<float> RabbitsM;
vector<float> RabbitsF;
int sex = rand() % 2 + 1;
if (sex == 1)
{
int color = rand() % 5;
if (color == 1)//brown
{
RabbitsM.push_back(Rabbit());
}
else if (color == 2)//black
{
RabbitsM.push_back(Rabbit());
}
else if (color == 3)//white
{
RabbitsM.push_back(Rabbit());
}
else if (color == 4)//spotted
{
RabbitsM.push_back(Rabbit());
}
}
else
{
int color = rand() % 5 + 1;
int color = rand() % 5;
if (color == 1)//brown
{
RabbitsF.push_back(Rabbit());
}
else if (color == 2)//black
{
RabbitsF.push_back(Rabbit());
}
else if (color == 3)//white
{
RabbitsF.push_back(Rabbit());
}
else if (color == 4)//spotted
{
RabbitsF.push_back(Rabbit());
}
}
}
}
} 有人可以帮助我!
答案 0 :(得分:1)
通常,您要做的是将性别和颜色添加为Rabbit
类的属性(成员变量),并将所有兔子存储在一个集合vector<Rabbit>
中。正如在其他答案中已经提到的,这也可以解决尝试将Rabbit
放入float
集合的问题。
答案 1 :(得分:0)
正如@James Root在评论中指出的那样,您正在尝试向Rabbit
添加std::vector<float>
。您需要将std::vector
更改为std::vector<Rabbit>
,然后才能向其添加Rabbit
:
vector<Rabbit> RabbitsM;
vector<Rabbit> RabbitsF;
RabbitsM.push_back(Rabbit());