Ok, so I have a small question. I've got this program I'm working on (should be obvious where I got the idea once you see the terms, don't tell me how to finish it please!) and it works fine for the most part.
The area I'm having an issue with has to do with passing a vector based on a custom class to a function whose sole purpose is to use a custom display function based on that class. It's a convenience thing, since I'm going to be printing out the results pretty regularly -- if not to the screen, then to a file or something.
When I use the vector and loop (commented out, but you can see it) inside the main() it does exactly what I want. When I use the bunnyshow() it gives me linker errors (specifically LINK2028, LINK2019 and LINK1120). I've tried passing it by pointers and got the same thing, so I'm fairly certain the problem is that I don't have the syntax right for the argument of the function. The googles do nothing, so I came here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
void bunnyshow(int &pnumbun, vector<Rabbit> bunnies);
int main() {
srand (time(0));
int numbun=0;
vector<Rabbit> bunnies;
cout << "how many: ";
cin >> numbun;
for (int i=0; i<numbun; i++) {
Rabbit * p = new Rabbit();
p ->birth();
bunnies.push_back(*p);
}
//for (int i=0; i < numbun; i++)
// bunnies.at(i).display();
bunnyshow(numbun, bunnies);
}
//this function is the problem child
void bunnyshow(int numbun, vector<Rabbit> bunnies) {
for (int i=0; i < numbun; i++)
bunnies.at(i).display();
}
|
***The following is totally extraneous and can be ignored by serious question answerers.***
I'm very proud of having figured out this part without any help:
1 2 3 4 5
|
for (int i=0; i<numbun; i++) {
Rabbit * p = new Rabbit();
p ->birth();
bunnies.push_back(*p);
}
|
I feel like that's pretty good for a total newbie who only has online resources (half of which seem to think that character arrays are tots better than strings *boggle*). I admit, I squealed like a girl (in public!) when it worked.
But I don't actually know what this thing that I've done is called, which makes researching it a pain in the butt. I've been calling it dynamic object creation, because that sounds hot, but I have no idea since I'm not seeing it show up in google.
Also, I think I might be better off with a list in an environment where I'm regularly creating and destroying objects. I should really research it so that I can internalize the reasons, but I'd love any commentary on the pros and cons.