#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14

    RE: Initializing object of a class and using methods via a vector


    As an inexperienced c++ programmer, I'm having difficulties implementing a driver program to work on a class (animal). Just to keep things simple I will show only the code which I suspect is causing the problem:

    int main ()
    {

    string name, *animalname;

    int numanimals

    cout << "Enter number of animals: ";
    cin >> numanimals;

    vector <string> animalnames;

    for (int i = 0; i < numanimals; i++)
    {
    cout << "Enter animal's name: ";
    cin >> name;

    animalnames.push_back (name);

    *animalname = animalnames[i];

    Animal*animalname;

    animalname->setName(name);

    cout << animalname->getName();
    }
    }




    Although I have got this driver program to compile, when I execute it, just after I have typed the 1st animal's name, I get the error message "core dumped"! What I'm trying to achieve is a program which determines the number of animals, then for each animal an input is asked to determine the animal's name. The program should then initialize an object in the class 'Animal' giving the object the name which the user has inputted. Then once the object has been initialized, the method in the 'Animal' class called 'setName' should be executed so that the object which the method operates on is the name that the user has typed in for the animal. Finally an output should be displayed showing the name that has just been set into the object and again the 'getName' method should operate on the object which corresponds to the name of the animal that has just been entered.

    Although I beleive I am getting closer to solving this problem, I would appreciate any ideas in where I am going wrong.

    Maz.
    Last edited by markb_1984; March 14th, 2003 at 05:16 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    I didn't know anything about vectors until a few minutes ago, but the basics seem easy enough. Sorry, I have no idea what a "driver" program is: is that just a test program that exercises the operations of the class? I'm unclear what you 're trying to do. Your code shows you creating a vector of strings, but it seems to me you want to create a vector of Animals(or pointers to Animals) because the animals will store the strings, so you need to keep the Animals organized in some fashion. Does that sound right?

    A point on good practice: you don't want to have variable names so similar they are hard to tell apart like animalname and animalnames, and it's good practice to preceed pointer names with a "p" e.g. panimalname. Actually, I'm surprised your code compiled because you declare a string pointer animalname, and then you declare an Animal pointer animalname. My VC++6.0 compiler would flag that as an error. Furthermore, you don't declare variables inside loops because they just get declared over and over.
    Last edited by 7stud; March 16th, 2003 at 10:42 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    I wrote a program to do what I think you want, and it works. The hardest part for me to figure out--and looking at your code, I think it was confusing for you to--was how to create Animal objects and then assign them to the vector. Since you don't know how many Animal objects you're going to need at compile time, you can't declare an appropriately sized array for them. You could declare an array with a size bigger than you'll ever need, but it seems redundant to create an array of objects and then just assign them to a vector. So, the next possibility to consider is to create Animal objects dynamically at runtime once the number of animals is known. That way you could create the exact sized array you need, but once again it seems redundant to create an array of Animal objects and then assign them to a vector, but you certainly could do that.

    My solution involved declaring an Animal object outside the for-loop that reads in the names. Then, I just used the setName() function for that object everytime through the loop to change the name, and I assigned the object to the new vector index each time through the loop, and it seemed to work fine. At the end of the loop, to prove everything in the vector was set up correctly, I set up another loop to print out the name member of the object at each vector index using getName() and it worked fine, e.g. Animal_vector[0]->getName();

    I'm not really sure why declaring only one object and assigning it over and over to the vector with the name changed works. I would think internally the vector would store a pointer to the object, so if you changed the name member of the object, each name at each index position would change too, so at the end, all the objects would have the same name. However, I know the vector functionality takes care of a lot of stuff, so somehow it must take care of that potential problem too.
    Last edited by 7stud; March 17th, 2003 at 02:20 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    I just realized how it's supposed to be done. After you read in the size, just declare a vector of type Animal with the user input as the size like this:

    vector<Animal> barn(size);

    just like you were declaring an array, but with an array you couldn't do that because the size has to be known at compile time i.e before the user enters the size. That's one demonstration of how easy vectors are to use. I don't think I'm going to be using arrays anymore.

    All the Animals in the vector will be created by the default constructor, so if you want, define a default constructor which assigns "empty" for the name. After you declare the vector, you can display all the animal names to verify they are "empty".

    Then in your loop, read in the names for the Animals, and use the vector index to call the setName() function on the Animal at that index position, with the argument being the name the user entered, e.g. barn[0].setName(name);

    Simple.
    Last edited by 7stud; March 17th, 2003 at 04:29 AM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14

    animal class


    Yeah, I think i've almost solved it, but need a little help understanding what is missing in me code:

    string name;

    int numanimals;

    cout << "Enter the number of Animals: ";
    cin >> numanimals;

    vector <Animal> animalnames(numanimals);

    Animal anAnimal;

    for (int i = 0; i < numanimal; i++)
    {
    cout << "Enter Animal number " << i + 1 << " 's name: ";
    cin >> name;

    animalnames.push_back (name);

    animalnames[i].setName(name);

    cout << animalnames[i].getName();
    }

    On compiling this I get an error message refering to the line:

    animalnames.push_back (name);

    The error message states:

    'no matching function for call to `std::vector<Animal, std::allocator<Animal> >::push_back(std::string&)

    Any ideas what's wrong please?
    Last edited by markb_1984; March 17th, 2003 at 07:39 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Hi,

    Try reading my last post again. Keep in mind that when you declare the vector, all the Animals are created for you, and all you have to do is set their names.

    I think your error has to do with the fact you are trying to put a string type into a vector that is an Animal type. Also, make sure you include the header file <vector>.
    Last edited by 7stud; March 17th, 2003 at 10:33 PM.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    154
    Rep Power
    14
    Thanks for the help, it really was straightforward in the end. The hardest part was understanding that vector <Animal> is actually a collection Animal objects, and in each case storing an animal object mimics (and therefore replaces the need of) the object initialisation i.e. Animal anAnimal, Animal anotherAnimal, etc...

    I think I'll stick to vectors instead of arrays as well, there an awful lot neater to use!

    Maz
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    "The hardest part was understanding that vector <Animal> is actually a collection Animal objects, and in each case storing an animal object mimics (and therefore replaces the need of) the object initialisation i.e. Animal anAnimal, Animal anotherAnimal, etc"

    I was hoping the example:

    barn[0].setName(name);

    would lead you in the right direction. My book always demonstrates object creation by putting a statement like this in the default constructor:

    Animal()
    {
    cout<<"Animal constructor called.";
    }

    Then when you declare your array or vector, you will see displayed to your screen a constructor call for each animal in the vector confirming what's going on. It can be a handy thing to do, if you're having trouble understanding what's going on.

    Anyway, thanks for teaching me about vectors.

IMN logo majestic logo threadwatch logo seochat tools logo