By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

STL Vectors call by reference using vectors of structure

Digital Don
P: 19
I have a problem sending the Vectors as Referencial parameters..i.e. I am having a struct vector

Expand|Select|Wrap|Line Numbers
  1.  
  2. struct board
  3. {
  4.      int r;
  5.      int c;
  6. };
  7.  
  8. void rd(vector <struct board> cp)
  9. {
  10. cout<<"\nr="<<cp[0].r;
  11. cp[0].r=8;
  12. }
  13. //---------------------
  14. /*This Read function should catch the vector reference and 
  15. any modification done to this cp should affect the cpy in main()*/
  16. //---------------------
  17. void read(vector <struct board> cp)
  18. {
  19. for(int i=0;i<4;i++)
  20. for(int j=0;j<4;j++)
  21. {
  22. struct board x;
  23. x.r=i;
  24. x.c=j;
  25. cp->push_back(x);
  26. rd(*cp);
  27. cout<<"\nr="<<cp[0].r;
  28. //---------------------------------------
  29. /*How do I access the r and c in the cp? I have tried a lot of thinsg but
  30. was unsuccessful in accessing the r and c in cp[0]? */
  31. //---------------------------------------
  32. }
  33. void main()
  34. {
  35.  
  36. vector <struct board> cpy;
  37.  
  38. read(&cpy);
  39.  
  40. for(int i=0;i<4;i++)
  41. //for(int j=0;j<4;j++)
  42. cout<<"\nCpy took "<<cpy[i].r<<" , "<<cpy[i].c<<endl;
  43. //-------------------
  44. Any effects in the functions to cpy by call by 
  45. reference should affect this cpy too...
  46. }
  47.  
I have 2 problems....

1 is call by reference for vector array variables...

2 is accessing the r and c in the cpy struct variable through the pointer?


Please help....

I was actually trying to use the single dimentional array for declaring this i.e.
struct board x[50];

but this said that I was supposed to initialize all the variables as this was not easy I thought of shifting to vectors and now I have this problem...

Thank You in advance...

Regards,
Digital Don...
Mar 8 '07 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,510
you should be able to pass vectors by reference using & so
Expand|Select|Wrap|Line Numbers
  1. void read(vector <struct board> &cp)
  2. {
  3. for(int i=0;i<4;i++)
  4. for(int j=0;j<4;j++)
  5. {
  6. struct board x;
  7. x.r=i;
  8. x.c=j;
  9. cp.push_back(x);
  10. rd(cp);
  11. cout<<"\nr="<<cp[0].r;
  12. }
  13.  
and call it
Expand|Select|Wrap|Line Numbers
  1. vector <struct board> cpy;
  2. read(cpy);
  3.  
Mar 8 '07 #2

Expert 100+
P: 1,510

I was actually trying to use the single dimentional array for declaring this i.e.
struct board x[50];

Thank You in advance...

Regards,
Digital Don...
not sure what your problem is with an array, e.g.
Expand|Select|Wrap|Line Numbers
  1. void rd(struct board cp[50])
  2. {
  3. cout<<"\nr="<<cp[0].r;
  4. cp[0].r=8;
  5. }
  6. //---------------------
  7. /*This Read function should catch the vector reference and 
  8. any modification done to this cp should affect the cpy in main()*/
  9. //---------------------
  10. void read(struct board cp[50])
  11. {
  12. int index=0;
  13. for(int i=0;i<4;i++)
  14. for(int j=0;j<4;j++)
  15. {
  16. struct board x;
  17. x.r=i;
  18. x.c=j;
  19. cp[index++]=x;
  20. rd(cp);
  21. cout<<"\nr="<<cp[0].r;
  22. //---------------------------------------
  23. /*How do I access the r and c in the cp? I have tried a lot of thinsg but
  24. was unsuccessful in accessing the r and c in cp[0]? */
  25. //---------------------------------------
  26. }
  27. int main()
  28. {
  29.  
  30. struct board cpy[50];
  31.  
  32. read(cpy);
  33.  
  34. for(int i=0;i<4;i++)
  35. //for(int j=0;j<4;j++)
  36. cout<<"\nCpy took "<<cpy[i].r<<" , "<<cpy[i].c<<endl;
  37. //-------------------
  38. //Any effects in the functions to cpy by call by 
  39. //reference should affect this cpy too...
  40. system("pause");
  41. }
  42.  
arrays are passed by reference so you don't need the &
Mar 8 '07 #3

Digital Don
P: 19
The problem with arrays was that it was asking me to "initiallize the array" which I declared in Main()...and declaring a 'n' sized array is not a good idea...

Thanks for the help regarding the VECTORS...

not sure what your problem is with an array, e.g.
Expand|Select|Wrap|Line Numbers
  1. void rd(struct board cp[50])
  2. {
  3. cout<<"\nr="<<cp[0].r;
  4. cp[0].r=8;
  5. }
  6. //---------------------
  7. /*This Read function should catch the vector reference and 
  8. any modification done to this cp should affect the cpy in main()*/
  9. //---------------------
  10. void read(struct board cp[50])
  11. {
  12. int index=0;
  13. for(int i=0;i<4;i++)
  14. for(int j=0;j<4;j++)
  15. {
  16. struct board x;
  17. x.r=i;
  18. x.c=j;
  19. cp[index++]=x;
  20. rd(cp);
  21. cout<<"\nr="<<cp[0].r;
  22. //---------------------------------------
  23. /*How do I access the r and c in the cp? I have tried a lot of thinsg but
  24. was unsuccessful in accessing the r and c in cp[0]? */
  25. //---------------------------------------
  26. }
  27. int main()
  28. {
  29.  
  30. struct board cpy[50];
  31.  
  32. read(cpy);
  33.  
  34. for(int i=0;i<4;i++)
  35. //for(int j=0;j<4;j++)
  36. cout<<"\nCpy took "<<cpy[i].r<<" , "<<cpy[i].c<<endl;
  37. //-------------------
  38. //Any effects in the functions to cpy by call by 
  39. //reference should affect this cpy too...
  40. system("pause");
  41. }
  42.  
arrays are passed by reference so you don't need the &
Mar 8 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.