Jump to content
  • Advertisement
Sign in to follow this  
ceribik

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by ToohrVyk
Decide 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 Knight
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?


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

Share this post


Link to post
Share on other sites
Quote:
Original post by ceribik
Quote:
Original post by ToohrVyk
Decide 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 this post


Link to post
Share on other sites
Quote:
Original post by ceribik
Quote:
Original post by ToohrVyk
Decide 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 this post


Link to post
Share on other sites
Quote:
Original post by ceribik
Quote:
Original post by ToohrVyk
Decide 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 this post


Link to post
Share on other sites
Quote:
Original post by ceribik
Quote:
Original post by ToohrVyk
Decide 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 this post


Link to post
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 this post


Link to post
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!