5

I have two vectors, and I would like to fill the first one with the second. The vectors are declared like this:

vector<Vec3> firstVector;

Where Vec3 is a struct with float x, y, z. I have tried liked this with assign:

secondVector.assign(firstVector.begin(), firstVector.end());

But it stops and complains, that there is problem with the end(). I have also tried pushback, but of course it's not working.

As I read before I should do it with assign, but I don't know how to solve it properly.

EDIT:

The error message with insert and assign are the same:

this 0x00000000 ... std::vector > * const

[size] CXX0030: Error: expression cannot be evaluated
[capacity] CXX0030: Error: expression cannot be evaluated

And it points to Visual Studio's vector file to iterator end to the return. With insert it points to iterator begin.

THE CODE:

The first Vector is also part of a struct:

struct o3DModel
{
    vector<Vec3> v_Vertices;
};

struct Vec3 {
public:
Vec3() {}

Vec3(float X, float Y, float Z)
{
    x = X;
    y = Y;
    z = Z;
}

float x, y, z;
};

I declare the "o3DModel" struct above in my app class like this and send it to my loader class:

o3DModel *tfTable;

void TheApp::Init()
{
    objLoader->ImportOBJ(tfTable, "testcube.obj");
}

The objLoader class, where I successfully fill my "v_Vertices" vector, where "oModel" is the sent "tfTable":

bool OBJLoader::ImportOBJ(o3DModel *oModel, char *strFileName)
{
    FillObjData(oModel);
    ...
    return true;
}

void OBJLoader::FillObjData(o3DModel *oModel)
{
    oModel->v_Vertices.insert(oModel->v_Vertices.begin(), v_Vertices.begin(), v_Vertices.end());
    // This here with insert
    outFile2 << oModel->v_Vertices[0].x << "\n";
}

Hope this helps.

matthew3r
  • 682
  • 4
  • 9
  • 26
  • 3
    Your code should work (although I disagree that you "should do it with assign"). Can you post the exact error message you receive? – Robᵩ Mar 28 '12 at 17:01
  • 2
    How is `secondVector` declared, and what exactly is the error message? (Also, a simple `secondVector = firstVector` would do the same thing). – Mike Seymour Mar 28 '12 at 17:02
  • I see I should post the error message. I edit my question with it. – matthew3r Mar 28 '12 at 17:08
  • You are dereferencing a NULL pointer. The problem is not in the code that you have shown us, but in the code that you haven't. Please reduce your program to the smallest possible program that still demonstrates the error, and then post that very small, **complete** program into your question. See http://sscce.org/ for more information. – Robᵩ Mar 28 '12 at 17:13
  • If not the complete program, but I will reduce everything and post it. – matthew3r Mar 28 '12 at 17:16
  • Possible duplicate of [fast way to copy one vector into another](https://stackoverflow.com/questions/644673/fast-way-to-copy-one-vector-into-another) – hmofrad Jul 19 '18 at 22:11

5 Answers5

8

If you want secondVector to take on all of the values of firstVector and no others,

secondVector = firstVector;

If you want each of the elements of firstVector to be added to the end secondVector:

secondVector.insert(secondVector.end(), 
                    firstvector.begin(), firstVector.end());

If you want each of the elements of firstVector to be added to the beginning of secondVector:

secondVector.insert(secondVector.begin(), 
                    firstVector.begin(), firstVector.end());
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
4

Or if you don't want to do it in the ctor use

secondVector = firstVector

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
2

This should work:

vector<Vec3> secondVector(firstVector.begin(), firstVector.end());

That's the case when you're interested in using the vector constructor.

Kiril
  • 39,672
  • 31
  • 167
  • 226
1

You second vector needs to be declared as a vector of vectors of Vec3 strucs.

    vector<vector<Vec3>> secondVector;
    secondVector.push_back(firstVector);
TVOHM
  • 2,740
  • 1
  • 19
  • 29
-1

Okay, I have solved it, but with a little roundabout. In FillObjData I have created a new Vec3, created an if, gived the first vector x, y, z coordinate to the new Vec3, and pushed the Vec3 into the vector. And when I declared my struct I have declared it real not as a pointer. Now it works perfectly.

matthew3r
  • 682
  • 4
  • 9
  • 26