Hello,
I want to pass a vector<string> to a function as a pointer, manipulate it and pass it back out, but can't get it to work.
I have found postings about it on the web but nothing seems to help.
This is how my code looks. Simplified.
I then try to call the function like so:
// class.cpp
It compiles and links but when I try and trigger the function it crashes and the XCode debugger kicks in.
Strangely(for me), I can get it to work if I do the following. Although I can't do this, I need to use my class variable.
EDIT: I have also tried casting names as, vector<string>* and then passing to function without the '&'. But I still get a crash.
Can anyone see what I am getting wrong?
Any help appreciated.
cheers
fred
I want to pass a vector<string> to a function as a pointer, manipulate it and pass it back out, but can't get it to work.
I have found postings about it on the web but nothing seems to help.
This is how my code looks. Simplified.
Code:
// class.h
class hitList
{
vector<string> names;
}
// function.h
void AddOneNameFunc(vector<string> * names);
//function.cpp
void AddOneNameFunc(vector<string> * names)
{
// ...manipulate names
}
I then try to call the function like so:
// class.cpp
Code:
void hitList::WhenAngry()
{
AddOneNameFunc(&names);
}
It compiles and links but when I try and trigger the function it crashes and the XCode debugger kicks in.
Strangely(for me), I can get it to work if I do the following. Although I can't do this, I need to use my class variable.
Code:
void hitList::WhenAngry()
{
vector<string> tmpNames;
vector<string>* pTmpNames = &tmpNames;
AddOneNameFunc(pTmpNames);
}
EDIT: I have also tried casting names as, vector<string>* and then passing to function without the '&'. But I still get a crash.
Can anyone see what I am getting wrong?
Any help appreciated.
cheers
fred