-3

I am trying to make a rabbit breeding program in c++. I keep getting an error under the period in this line...

RabbitsM.push_back(Rabbit());

I need to be able to put in whether the rabbits are female or male and what color they are. So what I was thinking is that I make two vectors (male and female) and I would like to have 4 slots in the vectors to have the color of the offspring (brown, white, black and spotted). The error I keep getting is...

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

Here is my source code...

  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());

            }
        }
    }
}

} Can someone please help me!

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
  • 1
    Are you trying to put a rabbit in a vector of floats? – Weak to Enuma Elish Nov 24 '15 at 23:24
  • Please also add your declaration of `RabbitsM` and `Rabbit` – Tas Nov 24 '15 at 23:24
  • Here is what I have so far in my source code. See above. – Tara Mallak Johnson Nov 24 '15 at 23:26
  • `int Rabbit::main()`???? I don't think `main` can be a member function. Why do you want to store rabbits as floats? – Weak to Enuma Elish Nov 24 '15 at 23:29
  • Rabbit is the name of the class. I am going off of my instructors instructions and my book. He has told us to use the line. Should it be RabbitsM or RabbitsF instead of Rabbit? – Tara Mallak Johnson Nov 24 '15 at 23:32
  • You're trying to add a `Rabbit` to a collection of `float`s (`vector`). A rabbit is not a float. – MicroVirus Nov 24 '15 at 23:32
  • so it should be an int? I am not sure how many will be in the vector because they are breeding so I thought a float would be better. – Tara Mallak Johnson Nov 24 '15 at 23:34
  • The type inside the brackets `vector` is what is stored, not the size. If you want to store numbers, use `vector`, floating point numbers, `vector`, rabbits `vector`, etc. – MicroVirus Nov 24 '15 at 23:36
  • A `vector` means it holds floats, a `vector` means it holds `Rabbit`s. Vectors always use a `std::size_t` for a size type. –  Nov 24 '15 at 23:37
  • I think the best answer for the OP is probably to [pull out a good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and start reading and writing small programs until they know enough of the terminology and methodology to understand the comments and answers. – user4581301 Nov 24 '15 at 23:51
  • @MicroVirus I would like to be reconsidered on opening the question. I have reworded the question. I am a new student to C++ and am not sure how it works in its entirety. I would like to thank those who did help. I am starting to understand. My instructor gave us a book that is online and it is not giving me much information. so I come on here to try and find help. – Tara Mallak Johnson Nov 25 '15 at 07:27
  • Your edit changes your question substantially. You should ask a **new question** instead if you have further issues. That will also give you a fresh start (and a fresh set of eyes) to look at your question. I've rolled back the edit to preserve the original question. – MicroVirus Nov 25 '15 at 12:13
  • As for the issue in your edit, you might want to check out the implementation of `set_values` in this [C++ Tutorial on classes](http://www.cplusplus.com/doc/tutorial/classes/) to see how your `setColor` should look, before posting a new question. – MicroVirus Nov 25 '15 at 12:21

2 Answers2

1

Typically, what you want to do is add the gender and the colour as a property (member variables) of the Rabbit class and store all rabbits in one collection vector<Rabbit>. As already mentioned in the other answer, this would also fix the issue with trying to put a Rabbit into a float collection.

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
0

As @James Root has pointed out in the comments, you are attempting to add a Rabbit to a std::vector<float>. You need to change your std::vector to be a std::vector<Rabbit> and then you will be able to add Rabbit to them:

vector<Rabbit> RabbitsM;
vector<Rabbit> RabbitsF;
RabbitsM.push_back(Rabbit());
Tas
  • 7,023
  • 3
  • 36
  • 51
  • Thank you I added that and now it takes that error away but I am getting a new one... Severity Code Description Project File Line Error LNK1561 entry point must be defined And I have a main which is what it is telling my online when I look it up. – Tara Mallak Johnson Nov 24 '15 at 23:40
  • @TaraMallakJohnson You have main _inside_ of your Rabbit class. Try putting it outside. –  Nov 24 '15 at 23:42
  • @ZirconiumHacker Thank you! – Tara Mallak Johnson Nov 25 '15 at 00:05
  • Okay that one is fixed and a new has shown up LOL I can't win. Rabbbit() is where the code is. RabbitsF.push_back(Rabbit()); This line is giving me 2 errors (1)'main::Rabbit': no appropriate default constructor available (2) incomplete type is not allowed – Tara Mallak Johnson Nov 25 '15 at 00:16
  • @TaraMallakJohnson You must have a default constructor for the `Rabbit` class. Otherwise, you cannot put it into a vector. –  Nov 25 '15 at 03:00