How do I call a function that returns a vector pointer
Aug 28, 2010 at 1:17am UTC
In the following code I have made a standard call to a function that returns a vector pointer. Line 26 is obviously incorrect and I was wondering if anyone could assist.
Thanks for any help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> naAllVector;
vector<string>::iterator it;
vector<string> saaAllVector;
vector<string>::iterator ite;
vector<string>* getvector (string idtag);
int main()
{
saaAllVector.push_back ("ag" );
saaAllVector.push_back ("bp" );
naAllVector.push_back ("cn" );
naAllVector.push_back ("sw" );
string idtag = "ag" ;
string genericvector = getvector(idtag); //this is problem line
cout <<"String = " ;
for (unsigned i=0; i< genericvector.size() ; i++)
cout <<", " << genericvector[i];
system ("pause" );
return 0;
}
vector<string>* getvector (string idtag)
{
if ((idtag == "ac" ) ||(idtag == "cn" ) ||(idtag == "ep" ) || (idtag == "ws" ) || (idtag == "ns" ) ||(idtag == "sw" ) ||(idtag == "ss" ) ||(idtag == "ok" ))
{ vector<string>*vectorptr = &naAllVector; return vectorptr; }
if ((idtag == "ca" ) ||(idtag == "cl" ) ||(idtag == "bp" ) || (idtag == "bz" ) || (idtag == "ag" ) )
{ vector<string>* vectorptr; vectorptr= &saaAllVector; return vectorptr;}
}
Last edited on Aug 28, 2010 at 1:18am UTC
Aug 28, 2010 at 2:19am UTC
Why are you putting a vector pointer into a string? A vector pointer should go into another vector pointer.
Aug 28, 2010 at 2:26am UTC
Like this???
vector <string>* genericvector = getvector(idtag);
But then how do I write the lines of code after line 26 which refer to the genericvector??
ie; the lines...
1 2 3 4
cout <<"String = " ;
for (unsigned i=0; i< genericvector.size() ; i++)
cout <<", " << genericvector[i];
Im struggling here... sry
Aug 28, 2010 at 2:29am UTC
1 2
for (unsigned i=0; i< genericvector->size() ; i++)
cout <<", " << (*genericvector)[i];
Yeash, I had to do this exact same thing myself. ;)
Last edited on Aug 28, 2010 at 2:30am UTC
Aug 28, 2010 at 3:03am UTC
Thanks LB. I'm teaching myself and Ive found no comprehensive tutorial or source that covers some of the vector problems etc that I have. Many tutorials just have bits and pieces and trying to fit it all together...
Thanks again, mate.
Topic archived. No new replies allowed.