• Advertisement
Sign in to follow this  

copying a vector not working when doing it more than once?

This topic is 2778 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is strange but I have a class that stores a std::vector, but the vector is created outside the class, then passed into the class. My intention is to not pass a pointer of the vector to the class but make a whole new copy so maybe I'm not doing this right.

If I do it once, the first class works just fine, but if I do it to more then one class, it doesn't copy the vector and any of the class past the first one.



std::vector vector;
/** push a bunch of values into vector */

class CLASS
{
std::vector classVector;

void addVector(std::vector vec) {
classVector = vec;
};
};

CLASS c1;
c1.addVector(vector);

/** That class works fine and I can use the vector */

CLASS c2;
c2.addVector(vector);

/** Ass soon as this class uses the vector it crashs */




I've steped through the debugger and it does indeed call the function and goes through the vector like the first one does. But it nevers adds anything to c2's vector, and the vectors size is always zero.

Am I just doing this wrong, or am I debugging it wrong?

Share this post


Link to post
Share on other sites
Advertisement
Idk if thats psuedo code, but it should looks something like this :


template<typename T >
class Foo{
std::vector< std::vector<T> > fooVec;

void addVector(const vector<T>& vec){
fooVec.push_back(vec);
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Concentrate
Idk if thats psuedo code, but it should looks something like this :


template<typename T >
class Foo{
std::vector< std::vector<T> > fooVec;

void addVector(const vector<T>& vec){
fooVec.push_back(vec);
}
}


my vector doesn't store vectors, and I'm not trying to reinvent the push_back() function, I'm trying to copy the entire vector.

Share this post


Link to post
Share on other sites
why don't you use assign?


void addVector(std::vector vec)
{
classVector.reserve(vec.size());
classVector.assign( vec.begin(), vec.end());
};


Share this post


Link to post
Share on other sites
Quote:
Original post by Juanxo
why don't you use assign?

*** Source Snippet Removed ***
I doubt that's the problem - the copy assignment operator should work fine. (Also, I don't know that the call to reserve() really accomplishes anything there.)

Share this post


Link to post
Share on other sites
What does the vector actually contain? If a class type, how are the copy and assignment operations implemented for the class?

Your code looks fine, but I suspect the crash may be more likely to be within the objects being copied.

Share this post


Link to post
Share on other sites
Doesn't std::vector require a type specifier?

As in: std::vector<someType> myVector;

I've never seen it used without one.

Share this post


Link to post
Share on other sites
Quote:
Doesn't std::vector require a type specifier?
Yup. (That's why I asked the OP to post a compilable example.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement