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.

7
  • Is that the complete source code? In particular, are there any header includes you're omitting? Commented Mar 21, 2014 at 0:00
  • Does not compile in g++ 4.8.1 either.. That is the entire source.. headers are <vector> @OP, use: std::array<std::vector<int>, 2>> for the typedef, it'll work.. Vector's allocator requires the type passed to be constructible, assignable, copyable. Commented Mar 21, 2014 at 0: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? Commented Mar 21, 2014 at 0: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. Commented Mar 21, 2014 at 0:01
  • The issue is that the element type is an array. See this SO post. Commented Mar 21, 2014 at 0:02

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.