3

I'm trying to do the following:

I have:

std::vector<std::vector<GLdouble[2]>> ThreadPts(4);

then I try to do:

 GLdouble tmp[2];
  while(step--)
  {


   fx   += dfx;
   fy   += dfy;
   dfx  += ddfx;
   dfy  += ddfy;
   ddfx += dddfx;
   ddfy += dddfy;
   tmp[0] = fx;
   tmp[1] = fy;
   ThreadPts[currentvector].push_back(tmp);
  }

But the compiler says:

Error 15 error C2440: 'initializing' : cannot convert from 'const GLdouble [2]' to 'double [2]' C:\Program Files\Microsoft Visual Studio 9.0\VC\include\vector 1211

How could I do this then? I'm using VS 2008 and don;t have std::array, and I don't have boost.

Thanks

jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • You should consider consulting your C++ book; if you don't already have one, you should consider getting one from [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – James McNellis Jul 07 '10 at 03:00

4 Answers4

3

A C-style array is not assignable, so it cannot be used as the value type of a vector.

If you are using at least C++11, you can #include <array> and use std::array. (Historically available in Visual C++ 2008 SP1 as std::tr1::array).

typedef std::vector<GLdouble[2]> pointList;
// Becomes
typedef std::vector<std::array<GLdouble, 2>> pointList;

https://en.cppreference.com/w/cpp/container/array

If you don't have that, you may be able to simply copy the Boost Array header into your project and use it on its own; it doesn't rely on many other parts of Boost, and those on which it does rely can be easily removed.

Richie Bendall
  • 7,738
  • 4
  • 38
  • 58
James McNellis
  • 348,265
  • 75
  • 913
  • 977
2

Instead of a raw array of 2 members, wrap it in a struct like a Point:

struct Point {
   GLDouble[2] coords;

   void setCoordinates(GLDouble x, GLDouble y)
   {
     coords[0] = x;
     coords[1] = y;
   }

   /* consider adding constructor(s) and other methods here,
    * if appropriate
    */
};

std::vector<std::vector<Point>> ThreadPts(4);

while(step--)
{
  fx   += dfx;
  fy   += dfy;
  dfx  += ddfx;
  dfy  += ddfy;
  ddfx += dddfx;
  ddfy += dddfy;

  Point p;
  p.setCoordinates(fx,fy);
  ThreadPts[currentvector].push_back(p);
}

It takes the same amount of memory as a 2-element array, and has more readable semantics than an array.

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • Agreed that this could be more readable in some scenarios, however, the array has the advantage of guaranteeing no padding between its elements, which is useful if the array has to be passed to another API (like OpenGL). – James McNellis Jul 07 '10 at 02:50
  • Edited to reflect wrapping in an array. – Jason S Jul 07 '10 at 02:51
0

You can use an inserter:

std::copy(tmp, tmp+2, std::back_inserter(ThreadPts[currentvector]));
Stephen
  • 47,994
  • 7
  • 61
  • 70
0

You could also use a std::pair

std::vector<std::vector<std::pair<GLdouble[2],GLdouble[2]> > >  ThreadPts(4);
Falmarri
  • 47,727
  • 41
  • 151
  • 191