C++ C++;在对象列表上使用“查找”

C++ C++;在对象列表上使用“查找”,c++,c++11,linked-list,find,C++,C++11,Linked List,Find,我似乎无法让std::find处理std::对象列表。我得到一个错误“与‘operator==’不匹配(操作数类型为‘Rabbit’和‘const int’)”。可以肯定的是,我需要使用一个对象迭代器和一个lambda函数,我完全不知道怎么做,我只需要一些指导。我最终要做的是迭代到列表中的特定位置,并从列表中的该对象中提取颜色字符串。 编辑以澄清问题并简化代码 #include <iostream> #include <list> #include

我似乎无法让std::find处理std::对象列表。我得到一个错误“与‘operator==’不匹配(操作数类型为‘Rabbit’和‘const int’)”。可以肯定的是,我需要使用一个对象迭代器和一个lambda函数,我完全不知道怎么做,我只需要一些指导。我最终要做的是迭代到列表中的特定位置,并从列表中的该对象中提取颜色字符串。 编辑以澄清问题并简化代码

    #include <iostream>
    #include <list>
    #include <algorithm>
    #include "Rabbit.h"
    #include "Population.h"

    using namespace std;

    int main()
    {
        Population Tracker;
        Tracker.parentSeed(); //generate first random 8, and populate the list
        Tracker.Breed(); //generate children, not working nothing happening  as   getMother does not work
        return 0;
    }

    class Rabbit
    {
        protected:
            const std::vector<std::string> namesList
            {"Maxwell", "David", "Laura", "Sarah" , "Benjamin", "Carl",
             "Rick", "Maggie", "Glenn", "Daryl", "Michonne", "Roseita",
             "Leslie", "Randy", "Ethan", "Survan", "Leah", "Tisha", "Marcus"};

            const std::vector<std::string> colorList
            {"Red", "Green", "Blue",
             "Grey", "Tan", "Brown",
             "Calico", "White"};

        public:
            Rabbit(); //blank for the initial population
            Rabbit(int); // pass an int for color inherited from mother
            ~Rabbit();
            void getRabbit();
            void randomNumGen(int);
            std::string name;
            std::string color;
    };

    Rabbit::Rabbit()
    {
        std::random_device random; //random seed obj
        std::default_random_engine e1(random()); //get a random number seed
        std::uniform_int_distribution<int> name_dist(0, 17); // vector position of name
    std::uniform_int_distribution<int> color_dist(0, 7); // vector position of col
     color = colorList[color_dist(e1)];
     name = nameList[name_dist(e1)];
    }

    class Population
    {
        friend class Rabbbit;
        protected:

        public:
            Population();

            void popStats(int, bool);
            void popList(Rabbit);
            int getMother();
            void parentSeed(); // generate initial population.  All stats are random.
            std::list<Rabbit> femaleList;
            std::list<Rabbit> maleList;
            std::list<Rabbit>::iterator male_it;
            std::list<Rabbit>::iterator female_it;
    };

    int Population::getMother()
    {
       female_it++
       //do something to iterate list and get data from the object at that position.  Not to be sequential, just used that as example. 
    }

    void Population::Breed()
    {
        /*
            generate a new rabbit with color inhereited from the mother
            father does not matter as all other stats are random
        */
        if(maleList.size() > 2 && femaleList.size() > 2)
        {
            getMother(); // does nothing right now
            std::cout << "Breed Success!" << std::endl;
            Rabbit newRabbit;
            popList(newRabbit);
        }
    }

void Population::parentSeed()
{
    /*
        Generate the initial seed count
    */
    for (int i = 0; i < 8; i++)
    {
        Rabbit newRabbit;
        popList(newRabbit);
    }
}
#包括
#包括
#包括
#包括“Rabbit.h”
#包括“人口.h”
使用名称空间std;
int main()
{
人口跟踪器;
Tracker.parentSeed();//生成第一个随机8,并填充列表
Tracker.bread();//生成子项,不工作由于getMother不工作,所以什么也没有发生
返回0;
}
班兔
{
受保护的:
常量std::向量名称列表
{“麦克斯韦”、“大卫”、“劳拉”、“莎拉”、“本杰明”、“卡尔”,
“瑞克”、“玛吉”、“格伦”、“达里尔”、“米肖恩”、“罗西塔”,
“莱斯利”、“兰迪”、“伊桑”、“苏凡”、“利亚”、“蒂莎”、“马库斯”};
常量std::向量颜色列表
{“红色”、“绿色”、“蓝色”,
“灰色”、“棕褐色”、“棕色”,
“印花布”、“白色”};
公众:
Rabbit();//初始种群为空
兔子(int);//传递一个int表示从母亲继承的颜色
~Rabbit();
void getRabbit();
无效随机数(整数);
std::字符串名;
字符串颜色;
};
兔子::兔子()
{
std::random_device random;//随机种子对象
std::default_random_引擎e1(random());//获取随机数种子
std::uniform_int_dist name_dist(0,17);//名称的向量位置
std::uniform_int_dist color_dist(0,7);//列的向量位置
颜色=颜色列表[color_dist(e1)];
姓名=姓名列表[姓名区(e1)];
}
阶级人口
{
朋友级拉比;
受保护的:
公众:
人口();
void popStats(int,bool);
无效POP列表(兔子);
int getMother();
void parentSeed();//生成初始填充。所有统计信息都是随机的。
std::列表女性列表;
std::列表列表列表;
std::list::迭代器;
std::list::迭代器;
};
int填充::getMother()
{
女_it++
//做一些事情来迭代列表,并从该位置的对象获取数据。不是顺序的,只是作为示例使用。
}
无效人口::繁殖()
{
/*
生成一只新的兔子,颜色是从母兔身上遗传下来的
父亲并不重要,因为所有其他数据都是随机的
*/
if(maleList.size()>2和&femaleList.size()>2)
{
getMother();//现在什么都不做

std::cout编译器不知道如何区分
Rabbit
s和原始整数。因此,它告诉编译器需要通过为
Rabbit
对象和整数定义重载相等运算符来说明如何区分它们。您应该在代码:

bool
operator==(int const &i, Rabbit const &r) {
  ...
  return ?;
}

bool
operator==(Rabbit const &r, int const &i) {
  ...
  return ?;
}

(1) 请将代码简化为一个可以按原样编译的示例。(2)您试图用“位置”1查找
std::a
Rabbit
是什么意思?(3)请注意,您动态分配兔子,但从未取消分配,因此存在内存泄漏;不需要动态分配,即使用
new
。好的,谢谢。为了清晰起见,我编辑了问题和代码。