Hello,

I'm just wondering how you return a vector from a member function. I've looked online but had no luck. At the moment my function is set to long double, and when I tried setting it to std::vector it came up with lots of errors. Here is my code:

public:
long double evalx(vector<long double>, vector<long double>, vector<long double>);


.....
......



long double IFS::evalx(vector<long double> x, vector<long double>a, vector<long double>b)
{

    a[0] = 1.0;
    b[0] = 1.0;
    for(int i=1; i<=1; ++i)
    {
        a[i] = (matrix[0]*a[i-1] + matrix[1]*b[i-1] + matrix[2]);
    }
    x.push_back(a[i+1]);
    return x;

}

It is telling me
error:: cannot convert 'std::vector<long double>' to 'long double' in return
which makes sense, I just don't know how to tell it to return a vector. Sorry I'm still very new to c++. Any help would be gratefully received. Thank you.

Recommended Answers

All 14 Replies

The type before the function name is the return type:

std::vector<long double> yourFunction()
{
  vector<long double> a;
  return a;
}

Oh right, nice one thanks a lot.
Is there any way of keeping my class in there though? As in the IFS bit. Because otherwise it doesn't know what I'm talking about when I call matrix[]..?

Certainly:

std::vector<long double> IFS::yourFunction()
{
  vector<long double> a;
  return a;
}

Brilliant, thanks very much, that has saved me a lot of time that might have been spent guessing the different possible combinations, compiling, working out errors, guessing again etc. Cheers.

Oh dear sorry sorry sorry, wish i understood c++ more.

When I try to call it i get a warning that c++ has encountered a problem and has to close. if i have

std::vector<long double> IFS::yourFunction(vector<long double> b)

can i not simply call

f.yourFunction(b);

?
Sorry.

That is fine, but you wont get the returned value.

Why are you passing a vector<long double> as a parameter?

The function should be defined like this:

std::vector<long double> IFS::yourFunction()
{

}

then you should call it with:

IFS yourClass;
std::vector<long double> returnedValue = yourClass.yourFunction();

Oh man, i'm sorry to be annoying but i still get the generic warning myproject.exe c++ has encountered a problem and has to close, even though it all compiles fine. It only gets that error when i call it. Is there any obvious error here?

std::vector<long double> IFS::evalx()
{

    vector<long double>a;
    vector<long double>b;

    a[0] = 1.0;
    b[0] = 1.0;
    for(int i=1; i<=1; ++i)
    {
        a[i] = (matrix[0]*a[i-1] + matrix[1]*b[i-1] + matrix[2]);
    }

    return a;

}

and called with (when the error comes)

std::vector<long double> a = f_1.evalx();

i wish i didn't keep posting and managed to work it out myself, but i'm seriously running out of time. Thanks.

You can't do this:

vector<long double> a;
a[0] = 1.0;

'a' hasn't been given any memory.

You need to do:

vector<long double> a(1); // give 'a' exactly one element
a[0] = 1.0;

or much better:

vector<long double> a;
a.push_back(1.0); // extend 'a' as needed

It is starting to seem like you are just trying to get this assignment done instead of learning c++ - I hope this is not the case!

It is only that I am at the stage of pulling all nighters and still might not finish. I definitely feel like i am learning a lot though. I really appreciate your help, thank you.

The reason I was passing a vector as a parameter was because I am using this function in a for loop in int main. I don't want to create a new vector with each iteration of the for loop by declaring it inside the function. That is what I'd be doing isn't it?

Yes. In that case you want to pass the vector by reference, and not return anything:

void yourFunction(std::vector<long double> &yourVector) // note the &
{
 // modify yourVector

// don't return anything, the values have been modifying in the vector you have passed.
}

Then you call this with:

std::vector<long double> yourVector;
yourFunction(yourVector); // yourVector will be modified

David

But if I don't return anything then my iterator has nothing to work with for the next step.. Oh no!

Why not? You just use the same array (it's values are now different). Post the loop if you still don't understand.

Not to worry I have finally figured it out! I massively over complicated things. Thanks again.

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.