1

So I've tried searching here, but haven't quite found the same issue. I can't seem to figure out how to properly use this tracked vector. Ultimately, I want a vector of an array (length == 2) of vectors. It's not that I'm getting an index out of bound message, it's that when I try to compile, it says:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\memory(631): error C2440:     '<function-style-cast>' : cannot convert from 'int' to 'std::vector<_Ty> '
1>          with
1>          [
1>              _Ty=int
1>          ]


//code

int main() {

    typedef vector<int> feature_points[2];
    vector< feature_points >tracked;

    tracked.resize(10);
}

I suppose I could do vector<vector<vector<int>>>, but since the array portion will always be a length of 2, I'd like to just use it as an array of 2, thereby not having to check for index out of bounds exceptions.

Thank you for your thoughts and suggestions.

Community
  • 1
  • 1
user1145707
  • 99
  • 2
  • 5
  • Is that the complete source code? In particular, are there any header includes you're omitting? – user2357112 Mar 21 '14 at 00:00
  • Does not compile in g++ 4.8.1 either.. That is the entire source.. headers are `` @OP, use: `std::array, 2>>` for the `typedef`, it'll work.. Vector's allocator requires the type passed to be constructible, assignable, copyable. – Brandon Mar 21 '14 at 00:00
  • why not use std::array (or boost::array if no c++0x or c++11 support) for the fixed size array? or a point Type or a pair? – vlad_tepesch Mar 21 '14 at 00:01
  • What exactly do you mean by *"thereby not having to check for index out of bounds exceptions"* ? Accessing a vector (via `operator[]`) out of bounds is undefined behavior, exactly as it is with an array. Of course, in the case of vector, you do have the option of having an exception thrown, by using the `at()` member function. But just because an exception is thrown, doesn't mean you have to check for it. – Benjamin Lindley Mar 21 '14 at 00:01
  • The issue is that the element type is an array. See this [SO post](http://stackoverflow.com/questions/4612273/correct-way-to-work-with-vector-of-arrays). – user783920 Mar 21 '14 at 00:02
  • possible duplicate: http://stackoverflow.com/questions/4612273/correct-way-to-work-with-vector-of-arrays – a.lasram Mar 21 '14 at 00:16
  • Ah, my apologies, I for some reason didn't copy the includes. #include #include #include #include using namespace std; Also, "thereby not having to check for index out of bounds exceptions" all I meant by that is if the array is initialized to 2, then I always know the array will be 2 and therefore don't have to check the array length. Just the code would be neater, I guess not a complete deal breaker. – user1145707 Mar 21 '14 at 14:47

1 Answers1

2

Use std::array instead of the array For example

#include <array>
#include <vector>

//...
std::vector<std::array<std::vector<int>, 2>> tracked;

Or

#include <array>
#include <vector>

//...
typedef std::array<std::vector<int>, 2> feature_points;
std::vector< feature_points >tracked;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Ah, this was spot on. I guess I was just used to defining arrays by [] in the definition. Really appreciate this response and feel quite dumb for not realizing it. – user1145707 Mar 21 '14 at 14:49