<p>There are a couple of issues.  First you need to make sure that <code>veBuffer</code> has the correct type, so that probably means you need to cast it to <em>pointer to vertex</em>:</p>
<pre><code>const auto vertices = reinterpret_cast&lt;const Vertex*&gt;(reBuffer);
</code></pre>
<p>Second a range-based <code>for</code>-loop needs to know where the range ends and giving it a raw pointer does not provide that information, so you cannot use it here.  Instead you will need to know how many vertices there are and use a normal <code>for</code>-loop:</p>
<pre><code>const size_t numVerts = 32;
for (size_t i = 0; i &lt; numVerts; ++i) {
    const Vertex&amp; vert = vertices[i];
    ...
}
</code></pre>

CC BY-SA 4.0
2020-06-26T07:57:59.233Z
2020-06-26T07:57:59.233Z
trojanfoe
<p>Okay, so I'm currently using the IAGetVertexBuffers() method and I'd like to loop through some of the returned buffers to verify the contents.</p>
<p>However, I'm kind of new to DirectX and C++ in general. I tried using a a simple C++ 11 loop:</p>
<pre><code>for (const auto&amp; verts : veBuffer) {
        std::cout &lt;&lt; verts &lt;&lt; std::endl;
</code></pre>
<p>But I get an error saying this statement requires a suitable begin statement. Any suggestions would be appreciated at this point.</p>

CC BY-SA 4.0
2020-06-24T18:47:15.980Z
I am not a DirectX user, but I guess the problem here is probably that you used a pointer to some allocated memory (`new`/`delete`) as `veBuffer`. Have a look at into [this question](https://stackoverflow.com/questions/36973045/c-basics-ranged-based-for-loop-and-passing-c-style-arrays-to-functions). So using a regular for loop (`for (int i=0; i<SIZE, ++i)`) should already fix your problem.
wychmaster
CC BY-SA 4.0
2020-06-24T18:52:14.523Z
Alternatively, you can use a `std::vector` as `veBuffer`, resize it to the necessary buffer size and pass the data pointer to your DirectX function with `veBuffer.data()`.
wychmaster
<c++><directx11><buffers>
How do you loop through the contents of a buffer in DX11?
junfanbl
# How do you loop through the contents of a buffer in DX11?

(Asked by: username_0 on 2020-06-24)

Okay, so I'm currently using the IAGetVertexBuffers() method and I'd like to loop through some of the returned buffers to verify the contents.

However, I'm kind of new to DirectX and C++ in general. I tried using a a simple C++ 11 loop:

```
for (const auto& verts : veBuffer) {
        std::cout << verts << std::endl;

```

But I get an error saying this statement requires a suitable begin statement. Any suggestions would be appreciated at this point.

**Tags:** c++, directx11, buffers

> **Comments:**
> **username_1**: I am not a DirectX user, but I guess the problem here is probably that you used a pointer to some allocated memory (`new`/`delete`) as `veBuffer`. Have a look at into [this question](https://stackoverflow.com/questions/36973045/c-basics-ranged-based-for-loop-and-passing-c-style-arrays-to-functions). So using a regular for loop (`for (int i=0; i<SIZE, ++i)`) should already fix your problem.  
> **username_1**: Alternatively, you can use a `std::vector` as `veBuffer`, resize it to the necessary buffer size and pass the data pointer to your DirectX function with `veBuffer.data()`.  

---

## 1 answers

### Answer by username_2 on 2020-06-26. Score: 2

There are a couple of issues. First you need to make sure that `veBuffer` has the correct type, so that probably means you need to cast it to *pointer to vertex*:

```
const auto vertices = reinterpret_cast<const Vertex*>(reBuffer);

```

Second a range-based `for`-loop needs to know where the range ends and giving it a raw pointer does not provide that information, so you cannot use it here. Instead you will need to know how many vertices there are and use a normal `for`-loop:

```
const size_t numVerts = 32;
for (size_t i = 0; i < numVerts; ++i) {
    const Vertex& vert = vertices[i];
    ...
}

```


---

