Hi all, here is the issue I'm facing. I create a function to which I enter an integer and a string. It should then return a number of strings which number is unknown.So since the number is not known up front I use vector.
This is what I have and it gives me "address of local variable 'aux' returned":
string *my_function(int akr, string chor)
{
vector<string> weeks;
// ...
// code here where I calculate the vector weeks: a vector of strings
// ...
// Then, once I have weeks I use an auxiliary aux to convert to string as below:
string aux[weeks.size()];
for(int j=0; j<weeks.size(); j++)
{
aux[j] = weeks[j];
}
return aux;
}
int main(int argc, char *argv[])
{
//here I print the second string of my_function() with the selected
//parameters. It actually prints it but I still get the warning.
cout<<my_function(2,"FgS7DFd")[1]<<endl;
}
I understand that aux is local so it's being removed from the memory once the function run is over but
1.how do I get around that?
2.Also, notice how I used the auxiliary aux to convert the vector to a string because if I did return weeks; I was getting a compilation error. Any way I can avoid that?
string my_function(int& something, string aux)
{
vector<string> weeks;
// ...
// code here where I calculate the vector weeks: a vector of strings
// ...
// Then, once I have weeks I use an auxiliary aux to convert to string as below:
string aux[weeks.size()];
for(int j=0; j<weeks.size(); j++)
{
aux[j] = weeks[j];
}
return aux;
}
int main(int argc, char *argv[])
{
//here I print the second string of my_function() with the selected
//parameters. It actually prints it but I still get the warning.
cout<<my_function(2,"FgS7DFd")[1]<<endl;
}
But no, I actually use (int akr, string chor). I just didn't include it as it's not where I'm focusing. I use (int akr, string chor), and then come to the point where I have calculated the vector 'weeks'.
does aux have to be an array?
AFAIK the assignment operator works just fine on arrays, so you can pass in an array of strings, create a new array of strings in the function assign it to the passed in "array &"