LINK errors from passing a Vector to a function

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.
This is your declaration signature -> void bunnyshow(int &pnumbun, vector<Rabbit> bunnies)

This is you definition signature -> void bunnyshow(int numbun, vector<Rabbit> bunnies)

What do you see?...
That I am a <expletive deleted> moron?

Because that is indeed what I see. Wow, I must have done it wrong the first time somehow, then switched it to that and then never noticed it when I switched it back.

Well, at least I was right about it being a syntax error. Whee!
Aeon221 wrote:
That I am a <expletive deleted> moron?

Don't talk about yourself like that!

Aeon221 wrote:
I'm very proud of having figured out this part without any help:

Nice! Though, I have a couple of things to say about this...

(1) You don't have to allocate new memory yourself. The push_back function takes care of that. This means that doing it like this would also work:

1
2
3
4
5
6
for (int i=0; i<numbun; i++)
{
    Rabbit p;
    p.birth();
    bunnies.push_back(p);
}

(2) Instead of calling birth explicitly why don't you put the call inside the Rabbit constructor? Then you could simply do:

1
2
for (int i=0; i<numbun; i++)
    bunnies.push_back(Rabbit());

Or even better:

bunnies.resize(numbun); (*)

That's all I can see for now...

Info on dynamic memory allocation -> http://cplusplus.com/doc/tutorial/dynamic/
Info on vector -> http://cplusplus.com/reference/stl/vector/

EDIT: (*) Sorry about that... It's not a good idea to put the birth call in the constructor as push_back also calls the Rabbit constructor.
Last edited on
push_back only calls the copy constructor.
Oh, ok. Still, I won't modify my post. I thought about it for a while and I believe it's better to call birth explicitly.
It would depend on what "birth" does. If it just initializes some values and sets up the class invariants, it's basically the constructor.
Sure, but my guess is that it prints something. I think that's what the exercise asks.
Topic archived. No new replies allowed.