passing vector to a pointer?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2005
Posts: 11
Reputation: XxAndyxX is an unknown quantity at this point 
Solved Threads: 0
XxAndyxX XxAndyxX is offline Offline
Newbie Poster

passing vector to a pointer?

 
0
  #1
May 2nd, 2005
I have a project where I must use pointers to do a search on some accounts I've built up into a vector. My problem is whenever I try to pass the vector to a pointer I get an error. I've tried many different things but to no avail. Maybe one of you can tell me what I'm doing wrong?

This is basically what I'm trying to do:
  1. #include <vector>
  2. using namespace std;
  3.  
  4. double * func()
  5. {
  6. vector<double> vec;
  7. return vec;
  8. }
  9.  
  10. int main()
  11. {double * p;
  12. p=func();
  13.  
  14. return 0;
  15. }

The error I get:
test.cpp: In function `double* func()':
test.cpp:7: cannot convert `std::vector<double, std::allocator<double> >' to `
double*' in return
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 63
Reputation: marinme is an unknown quantity at this point 
Solved Threads: 1
marinme marinme is offline Offline
Junior Poster in Training

Re: passing vector to a pointer?

 
0
  #2
May 2nd, 2005
  1. double * func()
  2. {
  3. vector<double> vec;
  4. return vec;
  5. }

I don't think there is a typecast for double to vector<double>. What you should do is use a pointer to a vector<double> variable:
  1. vector<double> * func()
  2. {
  3. vector<double> vec;
  4. return vec;
  5. }

That should work, but no guarantees since I didn't test it. Also, you may wish to change your double pointer in main() to a pointer to vector<double>.

Now, if you are trying to use the pointer to double for data in the vector, you may wish to just use an iterator.
Yeah... I'm lazy. Deal with it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 11
Reputation: XxAndyxX is an unknown quantity at this point 
Solved Threads: 0
XxAndyxX XxAndyxX is offline Offline
Newbie Poster

Re: passing vector to a pointer?

 
0
  #3
May 2nd, 2005
For this assignment we are required to use a pointer to search through an array (or vector) of accounts. Why doesn't this work?

  1. vector<account> vec_account; //creates a vector of accounts
  2. vector<account> * p; //creates a pointer to a vector of accounts
  3. get_accounts(vec_account); //fills the vector with accounts
  4. p=vec_account; //stores the base address of the vector into the pointer -but I get an error? Logically it makes since to me...
  5.  
  6. ERROR I GET IS:
  7. code.cpp:266: cannot convert `std::vector<account, std::allocator<account> >'
  8. to `std::vector<account, std::allocator<account> >*' in assignment

Here is the search algorithm I'll be using to search through the vector of accounts:

  1.  
  2. for (match=0; p < (p+v.size()) && match==0; ++p)
  3. if (acct_num==p->read_acct_num()) match=1 //acct_num is given by the user
  4. i--;
  5. if (match==0) cout<<"\n\tACCOUNT NUMBER DOES NOT EXIST!\n";
  6. else {
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 63
Reputation: marinme is an unknown quantity at this point 
Solved Threads: 1
marinme marinme is offline Offline
Junior Poster in Training

Re: passing vector to a pointer?

 
0
  #4
May 3rd, 2005
It should work just using
p = &vec_account;
Yeah... I'm lazy. Deal with it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,442
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: passing vector to a pointer?

 
0
  #5
May 3rd, 2005
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iterator>
  5. using namespace std;
  6.  
  7. vector<double> *func()
  8. {
  9. static const double init[] = {1.0,1.5,2.0,3.3};
  10. vector<double> *vec = new vector<double>(init, init + sizeof init / sizeof *init);
  11. return vec;
  12. }
  13.  
  14. int main()
  15. {
  16. vector<double> *p = func();
  17. copy(p->begin(), p->end(), ostream_iterator<double>(cout, "\n"));
  18. return 0;
  19. }
  20.  
  21. /* my output
  22. 1
  23. 1.5
  24. 2
  25. 3.3
  26. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC