Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fredrum

macrumors newbie
Original poster
Dec 14, 2008
11
0
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.

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
 

fredrum

macrumors newbie
Original poster
Dec 14, 2008
11
0
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:

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

fredrum

macrumors newbie
Original poster
Dec 14, 2008
11
0
I am looping over a set range.
And I just tried:
(its not really a hitlist you see :) )

Code:
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

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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

macrumors newbie
Original poster
Dec 14, 2008
11
0
Sorry about the confusing shorthand.

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

Code:
// 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:

Code:
// 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

macrumors newbie
Original poster
Dec 14, 2008
11
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.