PDA

View Full Version : return vector<string> from function




fredrum
Mar 26, 2009, 05:11 AM
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.

// 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
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.

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



lazydog
Mar 26, 2009, 05:39 AM
Hi

What are you doing in AddOneNameFunc()... perhaps the problem is in there.


b e n

fredrum
Mar 26, 2009, 05:48 AM
Hi Ben,

hm, I don't think there should be a problem there, as it it working fine when I do as in my second call example above.

All I am doing with the 'names' variable in the function is:

for (i loop)
{
names->pushBack(localStringVector[i]);
}

lazydog
Mar 26, 2009, 06:00 AM
Hi Fred

Well there is no bounds checking on [] so if your index is out of range you'll get unpredictable behaviour.

b e n

fredrum
Mar 26, 2009, 06:04 AM
I am looping over a set range.
And I just tried:
(its not really a hitlist you see :) )

for (int i = firstFrame; i <= lastFrame; i++)
{
names->push_back("-");
}


Which also does not work.
Remember, all this works if I pass a pointer as in my second call snippet.

lazydog
Mar 26, 2009, 06:04 AM
Hi Fred, is this C++ or some other language that looks a bit like C++?

b e n

fredrum
Mar 26, 2009, 06:07 AM
Should be c++, I hope. Maybe some C'ey bits. Why, whats wrong?

lazydog
Mar 26, 2009, 06:15 AM
Ah sorry, your shorthand for the for loop and the ->pushBack() made me think twice.

Perhaps I've missed something but I can't see what's wrong from the code you've posted. Perhaps you could post some more code?

b e n

fredrum
Mar 26, 2009, 06:33 AM
Sorry about the confusing shorthand.

Well, now after your suggestions I have simplified my function down to this.
Literally.

// function.h
int AddOneNameFuncTest(vector<string>* names);

// function.cpp
int AddOneNameFuncTest(vector<string>* names)
{
names->push_back("-");
return 0;
}


And the call now looks like this:

// class.cpp
void hitList::WhenAngry()
{
AddOneNameFuncTest(&names);
}


That is exactly how my code now looks.
To me it seems like maybe it does not like me trying to pass the address(&) of my class member variable vector<string>.

Is it something about vectors that one can't do that?
Why does my second example work but not the other?

thanks a lot for helping
fred

fredrum
Mar 26, 2009, 06:52 AM
Ahh...Iv'e got it!

Embarrassingly, I tried to use the 'hitList' without having instantiated it(that what you call it?). The hitList was itself a pointer.

I never tried to use any of its member functions before without it having been instantiated by a different part of the program. I now tried to do it cold.
Bad. Re-design.

Strange that it half worked when I did that other thing.



Thank you very much Ben!

cheer
fred

lazydog
Mar 26, 2009, 06:59 AM
Great!

good luck

b e n