User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,439 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,881 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 8181 | Replies: 4
Reply
Join Date: Apr 2005
Posts: 11
Reputation: XxAndyxX is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
XxAndyxX XxAndyxX is offline Offline
Newbie Poster

passing vector to a pointer?

  #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:
#include <vector>
using namespace std;

double * func()
{
vector<double> vec;
return vec;
}

int main()
{double * p;
p=func();

return 0;
}

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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2005
Location: Oklahoma
Posts: 63
Reputation: marinme is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 1
marinme marinme is offline Offline
Junior Poster in Training

Re: passing vector to a pointer?

  #2  
May 2nd, 2005
double * func()
{
vector<double> vec;
return vec;
}

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:
vector<double> * func()
{
     vector<double> vec;
     return vec;
}

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  
Join Date: Apr 2005
Posts: 11
Reputation: XxAndyxX is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
XxAndyxX XxAndyxX is offline Offline
Newbie Poster

Re: passing vector to a pointer?

  #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?

vector<account> vec_account; //creates a vector of accounts
vector<account> * p; //creates a pointer to a vector of accounts
get_accounts(vec_account); //fills the vector with accounts
p=vec_account; //stores the base address of the vector into the pointer -but I get an error?  Logically it makes since to me...

ERROR I GET IS:
code.cpp:266: cannot convert `std::vector<account, std::allocator<account> >'
   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:

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

Re: passing vector to a pointer?

  #4  
May 2nd, 2005
It should work just using
p = &vec_account;
Yeah... I'm lazy. Deal with it.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,471
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: passing vector to a pointer?

  #5  
May 2nd, 2005
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;

vector<double> *func()
{
   static const double init[] = {1.0,1.5,2.0,3.3};
   vector<double> *vec = new vector<double>(init, init + sizeof init / sizeof *init);
   return vec;
}

int main()
{
   vector<double> *p = func();
   copy(p->begin(), p->end(), ostream_iterator<double>(cout, "\n"));
   return 0;
}

/* my output
1
1.5
2
3.3
*/
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 12:53 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC