Public Group

# C++: Passing vector references (problem)

This topic is 4095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello again, I'm having problems passing a vector reference into a function, and I'm fairly certain that I am doing it correctly. Here are the details:
bool deleteEmployee(vector<Employee>& employees, string& lastName);

// Not sure if this is helpful...
vector<Employee*> Employees;

// Generates c2664.
if(deleteEmployee(Employees, lastName))


Quote:
 Error 1 error C2664: 'deleteEmployee' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'std::vector<_Ty> &'
Any ideas would be appreciated. Thanks :)

##### Share on other sites
Decide yourself. Employee* (pointer) or Employee (value) ?

##### Share on other sites
This works for me O_o
http://rafb.net/p/chITyq11.html
There could something else in your code can you post the relevant part?

##### Share on other sites
Quote:
 Original post by ToohrVykDecide yourself. Employee* (pointer) or Employee (value) ?

I'm not quite sure what you mean. Could you explain a bit more?

Quote:
 Original post by Black KnightThis works for me O_ohttp://rafb.net/p/chITyq11.htmlThere could something else in your code can you post the relevant part?

That won't work for me, as I'm dealing with polymorphism/inheritance .

##### Share on other sites
Quote:
Original post by ceribik
Quote:
 Original post by ToohrVykDecide yourself. Employee* (pointer) or Employee (value) ?

I'm not quite sure what you mean. Could you explain a bit more?

bool deleteEmployee(vector<Employee>& employees, string& lastName);// Not sure if this is helpful...vector<Employee *> Employees;

##### Share on other sites
Quote:
Original post by ceribik
Quote:
 Original post by ToohrVykDecide yourself. Employee* (pointer) or Employee (value) ?

I'm not quite sure what you mean. Could you explain a bit more?

You are declaring a vector of pointers vector<Employee*> but a function needs a reference to a vector of values vector<Employee>

##### Share on other sites
Quote:
Original post by ceribik
Quote:
 Original post by ToohrVykDecide yourself. Employee* (pointer) or Employee (value) ?

I'm not quite sure what you mean. Could you explain a bit more?

Certainly:

bool deleteEmployee(vector<Employee>& employees, string& lastName);                           ^^^^^^^^                        This is a value.// Not sure if this is helpful...vector<Employee*> Employees;       ^^^^^^^^^    This is a pointer.// Generates c2664.if(deleteEmployee(Employees, lastName))                  ^^^^^^^^^              This has type "vector of pointers", but is used              here with type "vector of values".

Moral of the story: use typedefs.

EDIT: Darn you, rip-off, that's the second time in five minutes!

##### Share on other sites
Quote:
Original post by ceribik
Quote:
 Original post by ToohrVykDecide yourself. Employee* (pointer) or Employee (value) ?

I'm not quite sure what you mean. Could you explain a bit more?

What he means is that you declare Employees as a vector of pointers to Employee , while deleteEmployee takes a vector of plain Employee objects. So either you change Employees to contain plain Employee objects instead of pointers, or, better since you say you are dealing with polymorphism, change deleteEmployee to take a vector of pointers.

Edit: Three posts before mine...

##### Share on other sites
Ah ok. All those explanations made sense (although the first one was sufficient :P). ToohrVyk, gets bonus points for the wonderful diagram.

Thank you all again.

##### Share on other sites
Quote:
 EDIT: Darn you, rip-off, that's the second time in five minutes!

I'm surprised, SiCrane is around. He usually beats me. Sometimes I think his machine is set up between the internet and the GDnet server. [smile]

Besides, what your post lacks in speed it makes up for it in usefulness (suggestion of typedefs, etc).

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633704
• Total Posts
3013457
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!