I have a problem where some vector returns are empty. Here is the basic setup:
//Foo.h
struct Foo{
int n;
double vals[2];
};
//MyClass.h
class MyClass{
private:
std::vector<Foo> classVector;
public:
std::vector<Foo> getPair(int i1, int i2);
};
//MyClass.cpp
std::vector<Foo> MyClass::getPair(int i1, int i2)
{
std::vector<Foo> toReturn(2);
toReturn[0] = classVector[i1 - 1];
toReturn[1] = classVector[i2 - 1];
return toReturn;
}
classVector is a vector that is already initialized and has data in it. I'm basically just trying to obtain two values from that vector and return them using this function. However, when I look at toReturn in debug mode once return is called, the size is reset to zero and the elements that had been copied over (I checked before the return call) are no longer in the vector.
I have a feeling it has to do with a shallow copy being passed on return. I tried creating my own copy constructor for Foo, but I deleted it when it didn't solve the problem.