vector <string *> v; passed by reference

Thread Solved

Join Date: Oct 2007
Posts: 60
Reputation: Grub is an unknown quantity at this point 
Solved Threads: 0
Grub Grub is offline Offline
Junior Poster in Training

vector <string *> v; passed by reference

 
0
  #1
May 26th, 2008
Hi, I have the following class and main method:
  1. class Employee
  2. {
  3. vector<string> v;
  4. string name;
  5. int i;
  6. public:
  7. Employee(){}
  8. Employee(string &n): name(n), i(0){}
  9.  
  10.  
  11. void read(vector<string *> &vec)
  12. {
  13. string n;
  14. for(int i = 0; i !=3; i++)
  15. {
  16. cin>>n;
  17. Employee e(n);
  18. vec.push_back(&n);
  19. }
  20. }
  21. };
  22.  
  23. int main()
  24. {
  25. vector<string *> v;
  26. Employee e;
  27. e.read(v);
  28.  
  29. for(int i = 0; i !=v.size();++i)
  30. {
  31. cout<<"In the main method loop"<<endl;
  32. cout<<*v[i]<<endl;
  33.  
  34. }
  35. cout<<v.size()<<endl;
  36. system("pause");
  37. return 0;
  38. }
Now upon trying to access the vector after I am back in the main method, the program crashes. Does anyone know why?

Many thanks
Last edited by Ancient Dragon; May 26th, 2008 at 2:27 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,612
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: vector <string *> v; passed by reference

 
0
  #2
May 26th, 2008
delete line 17 because it is a do-nothing line.

line 18 is pushing a pointer to an object that disappears from scope as soon as the function returns to main() -- the pointers are immediately invalidated. Why use a vector of pointers anyway? Just do this: vector<string> vect; and everything will work as you expect.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 60
Reputation: Grub is an unknown quantity at this point 
Solved Threads: 0
Grub Grub is offline Offline
Junior Poster in Training

Re: vector <string *> v; passed by reference

 
0
  #3
May 26th, 2008
many thanks
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 409 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC