Why is this wrong for the loop?

Visual Studio tells me that this is not true for the loop. Error messages:

  • bool type unexpectedly
  • ok - undeclared identifier
  • missing; before}

information on:

-recordset.Select return long -MoveNext a bool

for (size_t i = 0, bool ok = recordset.Select(Adress::getSQLStatement() + "Where A05.recid = %ld", i); ok; ok = recordset.MoveNext(), i++) {
    at(i).Save(recordset);
}
+4
source share
2 answers

It is like StenSoft . But you can define an anonymous structure in the first cycle of loops and initialize it.

#include <iostream>
using namespace std;

int main() {
    for (struct {size_t i; bool ok;} s = {0, true}; s.ok; ++s.i) {
        s.ok = s.i < 10;
        cout << s.i;
    }
    return 0;
}

But IMHO, while it works , it's more of a problem than it's worth it. Better rebuild your code.

+6
source

First, you can of course rewrite your loop like this:

{
    bool ok = recordset.Select(...);
    for (std::size_t i = 0; ok; ok = recordset.MoveNext(), ++i)
    {
        /* ... */
    }
}

- , for -loops, , , . , for -loops, . " " . , :

if (bool ok = recordset.select(...)) 
{
    for (std::size_t i = 0; ok; ok = recordset.MoveNext(), ++i) { /* ... */ }
}
else
{
    // handle initial error
}

, , ok:

if (recordset.select(...)) 
{
    for (std::size_t i = 0; ; ++i)
    {
        /* ... */

        if (!recordset.MoveNext()) break;
    }
}
else
{
    // handle initial error
}
+2

Source: https://habr.com/ru/post/1652845/


All Articles