1

I'm sure this question exists somewhere on this site, but I guess I'm not too sure what to search because I haven't found my answer yet. So I apolgoize if I am posting a duplicate.

Anyway, I am trying to create a vector of pointers that point to a vectors.

Here is what I have so far:

 float knot_vector1[] = {-1,-1,-1 0,1,1,1}; float knot_vector2[] = {-1, -1, 1, 1}; float knot_vector3[] = {-1, -1, -1, -1, 0, .5, .5, .5, 1, 1, 1, 1}; // initialize vectors with the arrays defined above vector<float> U1 (knot_vector1, knot_vector1 + sizeof(knot_vector1) / sizeof(knot_vector1[0]) ); vector<float> U2 (knot_vector2, knot_vector2 + sizeof(knot_vector2) / sizeof(knot_vector2[0]) ); vector<float> U3 (knot_vector3, knot_vector3 + sizeof(knot_vector3) / sizeof(knot_vector3[0]) ); vector<float> *ptr_u1 = &U1; // creating pointers that point to my vectors vector<float> *ptr_u2 = &U2; vector<float> *ptr_u3 = &U3; vector<vector<float>*> knotvectors[] = {ptr_u1, ptr_u2, ptr_u3}; 

Here is the error I receive:

error: conversion from 'std::vector<float>*' to non-scalar type 'std::vector<std::vector<float>*>' requested| 

So obviously something is wrong with the last line where I define my vector of pointers. What is the proper way to do this?

Thank you in advance.

4
  • I guess you really meant to type *ptr_u2 = &U2 and *ptr_u3 = &U3, not U1 all 3 times? Commented Jul 8, 2016 at 14:57
  • @underscore_d Yes, I just edited it. Thank you. Commented Jul 8, 2016 at 14:58
  • You need to remove the [] from the vector<vector> declaration as you are then declaring an array of vector<vector>. Commented Jul 8, 2016 at 14:59
  • You could create the 1st directly as {&U1, &U2, &U3}: you don't need to declare intermediate pointers just to put them into the vector. Commented Jul 8, 2016 at 15:07

2 Answers 2

3

This

vector<vector<float>*> knotvectors[] = {ptr_u1, ptr_u2, ptr_u3}; 

is an array of vector<vector<float>*>.

You want

vector<vector<float>*> knotvectors = {ptr_u1, ptr_u2, ptr_u3}; 
Sign up to request clarification or add additional context in comments.

3 Comments

I do not think that initialization works with my compiler. Would I have to use push_back() instead?
@MikeJamesJohnson Put your compiler into C++11 mode so that it supports std::initializer list. Btw, the = is not necessary.
It should work with C++11, if you're using C++03, you might have to use push_back(), yes.
3

It seems you mean the following

vector<vector<float>*> knotvectors = {ptr_u1, ptr_u2, ptr_u3}; 

Or maybe it would be better just to write

vector<vector<float>> knotvectors = { U1, U2, U3}; 

4 Comments

Does one have an advantage over the other?
@MikeJamesJohnson I do not know the task but I am not sure that you indeed need a vector of pointers to other vectors. Maybe you need a vector of vectors.:)
The 1st, in that it'll actually do what you want. The 2nd copies your vectors into a new container, instead of pointing to them. But yes... maybe you really want the 2nd ;-)
...and if you do, then equivalent to my latest comment on your OP about pointers - you wouldn't actually need to declare intermediate vectors just to put them into the final one. In fact, in this case, it might be an unnecessary overhead. You could construct directly in place, again using std::initializer_list, e.g.: std::vector< std::vector<float> > { {2.0, 4.2}, {9.3, 23948.3, 1.1}, {-2350823.905} };

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.