return vector<string> from function

Discussion in 'Mac Programming' started by fredrum, Mar 26, 2009.

  1. macrumors newbie

    Joined:
    Dec 14, 2008
    #1
    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
     
  2. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #2
    Hi

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


    b e n
     
  3. thread starter macrumors newbie

    Joined:
    Dec 14, 2008
    #3
    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]);
    }
     
  4. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4
    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
     
  5. thread starter macrumors newbie

    Joined:
    Dec 14, 2008
    #5
    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.
     
  6. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    Hi Fred, is this C++ or some other language that looks a bit like C++?

    b e n
     
  7. thread starter macrumors newbie

    Joined:
    Dec 14, 2008
    #7
    Should be c++, I hope. Maybe some C'ey bits. Why, whats wrong?
     
  8. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #8
    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
     
  9. thread starter macrumors newbie

    Joined:
    Dec 14, 2008
    #9
    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
     
  10. thread starter macrumors newbie

    Joined:
    Dec 14, 2008
    #10
    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
     
  11. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #11
    Great!

    good luck

    b e n
     

Share This Page