944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 25669
  • C++ RSS
May 2nd, 2005
0

passing vector to a pointer?

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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:
Quote ...
test.cpp: In function `double* func()':
test.cpp:7: cannot convert `std::vector<double, std::allocator<double> >' to `
double*' in return
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAndyxX is offline Offline
11 posts
since Apr 2005
May 2nd, 2005
0

Re: passing vector to a pointer?

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
marinme is offline Offline
63 posts
since Apr 2005
May 2nd, 2005
0

Re: passing vector to a pointer?

For this assignment we are required to use a pointer to search through an array (or vector) of accounts. Why doesn't this work?

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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 {
Reputation Points: 10
Solved Threads: 0
Newbie Poster
XxAndyxX is offline Offline
11 posts
since Apr 2005
May 3rd, 2005
0

Re: passing vector to a pointer?

It should work just using
p = &vec_account;
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
marinme is offline Offline
63 posts
since Apr 2005
May 3rd, 2005
0

Re: passing vector to a pointer?

C++ Syntax (Toggle Plain Text)
  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. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Trying To Find The Square Root Of Each Number Of A Array
Next Thread in C++ Forum Timeline: Sorting Algorithms using Time





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC