I have been struggling with this one for a day now and I cant seem to figure out if my problem is syntax or simple impossibility. The function in question looks something like this:

string *function( string temp[] )
{
    string sides[ 4 ];
    // code
    return sides;
}

and I am calling it like so:

string *tempSide = function( sides[ 0 ] );

sides[ 0 ] being an element of a matrix of strings. When I compile and run, I get this message:

terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create

I have never encountered this before, nor have I ever attempted to return a string array (which if I am not mistaken is itself matrix) or pass an element of a string matrix as an argument so I am fully willing to accept that I am just doing something silly. However a viable solution that wont require a full rewrite of the rest of my code (which I cant actually show) would be preferable. Thank you for your time.

have you tried using vectors? you could store the separate strings in a vector just like an array and pass the vector into the function. also is your function did you want to take in an array of strings or a single string at a time. you only need one string at a time i would just write string foo(string foobar)

have you tried using vectors? you could store the separate strings in a vector just like an array and pass the vector into the function. also is your function did you want to take in an array of strings or a single string at a time. you only need one string at a time i would just write string foo(string foobar)

As far as I have been able to determine, the function itself works fine and indeed I need the function to take in an array of strings. The length of the array is static however and a vector might work better for passing the string to the function, but I don't think that is the issue here. It seems to crash after the function runs so I am led to believe that I am doing something wrong with returning the array of strings. On the other hand, returning a vector instead might work. I just don't know what the function prototype should look like in that case.

well it could look something like

vector<string> foo(vector<string> foobar);

Thank you much, I will give that a shot. If I do have problems I will just start a new thread and mark this one as solved for the time being.

Ok, just thought I would give a quick update with the solution I came up with. Due to my inexperience with vectors and the apparent amount of re-factoring that would be needed to make it work with my current code, I ended up going with a solution I came up with while just driving around town. Instead of an array of strings being stored in a matrix of strings, I switched to using an array of custom data type which simply contained an array of strings. This solution might not work for all cases but since the number of strings I would need would never change, it worked perfectly. The kicker was that it added nearly nothing to the over all code length save for the definition of the object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.