openmp syntax error under VS10 - c++

I'm trying to use a simple #pragma omp parallel for under Visual Studio 10 and I get an error I don't understand
Here's what I do :
#pragma omp parallel for
for (int i(0); i < size; ++i)
{
// do some stuff
}
And I get these errors when compiling :
error C2059: syntax error : 'constant' // on the for() line
error C2059: syntax error : ';' // on the for() line
error C2143: syntax error : missing ';' before '{'
// repeat previous error for every { or } in file
fatal error C1004: unexpected end-of-file found // on last line of file
openmp support is activated in the compiler options. This code compiles and runs perfectly fine without openmp instructions.
I tried to nest the for loop in braces like this :
#pragma omp parallel for
{
for (int i(0); i < size; ++i)
{
// do some stuff
}
}
but then compiler tells me he expects a for loop right after the #pragma instruction.
Does anyone see what I can be doing wrong here ? It drives me crazy since I have already successfully used OpenMP within the same conditions in other programs.

I don't think object style initialisers are supported inside the for loop control block when OpenMP is active. You should rewrite your code as:
for (int i = 0; i < size; ++i)
In the second case the error is due to the fact that omp for requires an immediately following for loop and not a code block.

Related

Why doesn't this compile? (VC++ 2015, #pragma omp flush)

Here's a minimal case. Compile with "/openmp" on Visual C++ 2015.
#include <vector>
void main()
{
bool foo = false;
#pragma omp flush (foo)
std::vector<int> bar;
}
I get:
C2146 syntax error : missing ';' before identifier 'bar'
C2275 'std::vector<int,std::allocator<_Ty>>' : illegal use of this type as an expression
C2065 'bar' : undeclared identifier
If I comment out the #pragma, the error goes away.
If I replace std::vector with int, the error goes away.
If I put a ; on a line by itself below the #pragma, the error goes away.
The concise answer to the question "Why doesn't this compile?" is "it's a compiler bug". The bug report at:
https://connect.microsoft.com/VisualStudio/feedbackdetail/view/2420614
has been closed as "fixed".... "this item has been fixed in the current or upcoming version of this product [Visual Studio 2015]".

Compile error on VS2013 when range-based for loop and do while loop are used simultaneously

#include <vector>
int main()
{
std::vector<int> v;
for (int x : v) do {} while (0);
}
Compiling the code above on VS2013 will yield error C2059: syntax error : '}'. However, GCC can successfully compile the code.
To reproduce the error, the following requirements should be fulfilled:
Use range-based for loop;
Do not surround for-loop body by "{}";
Write a single "do while" statement in the for-loop body.
Any insight on this?
It's a bug, and it's fixed in VS2015.
You can use parentheses to avoid MSVC 2013 error. The following code is compiling fine in MSVC 2013 Update 4:
std::vector<int> v;
for (int x : v)
{
do
{
} while (0);
}

Error with range based loop in Qt

Get these errors :
C2143: syntax error : missing ',' before ':'
C2143: syntax error : missing ';' before '{'
Here is code:
void MainWindow::PrintDir(const QString &str)
{
QDir mDir(str);
QString buffer;
for(QFileInfo temp : mDir.entryInfoList()) //first error
{ //second error
buffer += temp.absoluteFilePath() + "\n";
}
ui->textEdit->setText(buffer);
}
I guess the reason is C++ standard? I try to include him in pro file like this CONFIG += c++11 but nothing happen and still get same errors. Whats wrong?
UPD:
As explained here, it seems like MSVC 2010 doesn't support C++ 11 range-based for loops. This is why you get this error. I recommend you upgrade to MSVC 2012 if you want to use C++11 range-based for loops.

why type casting on non-pointer struct give syntax error

I am using Visual C++ express 2008 try to compile code similar to below:
no problem
{
...
AVRational test = {1, 1000};
...
}
but has problem when it is as below:
{
...
AVRational test = (AVRational){1, 1000};
...
}
gave errors:
1>..\..\..\projects\test\xyz.cpp(1139) : error C2059: syntax error : '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '{'
1>..\..\..\projects\test\xyz.cpp(1139) : error C2143: syntax error : missing ';' before '}'
where AVRational (ffmpeg.org library) is defined as:
typedef struct AVRational{
int num; ///< numerator
int den; ///< denominator
} AVRational;
FFmpeg come with some pre-define value such as
#define AV_TIME_BASE_Q (AVRational){1, AV_TIME_BASE}
which is used as below
av_rescale_q(seek_target, AV_TIME_BASE_Q, pFormatCtx->streams[stream_index]->time_base);
will failed to compile on Visual C++ express 2008
It seem like the same code will be compiled with no error/warning on gcc compiler. Why I get this error on VC++? Is it a C/C++ standard way to do casting on struct value? Anyway I can avoid this error while still able to use the defined AV_TIME_BASE_Q?
Use av_get_time_base_q() instead of AV_TIME_BASE_Q for C++ or VS.
This was fixed in a patch
VC++ 2013 does not allow compound literals in C++ but it allows them in C. Options:
Rename your program with a .c suffix
Switch on the /TC flag for the program that does not compile.
The other alternative if you wish to keep to C++ is to change the declaration of AV_TIME_BASE_Q in the header file
static const AVRational AV_TIME_BASE_Q = {1, AV_TIME_BASE};
Then it will be using the constant instead of the compound literal.
For compound-literals errors in C++
wrong:
this->buffer.enqueue((tone_t) { duration, frequency });
correct:
tone_t tone = { duration, frequency };
this->buffer.enqueue(tone);

error C2143: syntax error : missing ',' before ':'

I'm trying to build am opensource game but keep getting this error when trying to build. I have been searching for the last half hour with nothing working here's the code the errors pointing to
void duel::restore_assumes() {
for(auto pcard : assumes)
pcard->assume_type = 0;
assumes.clear();
}
and the error is
Error 1 error C2143: syntax error : missing ',' before
':' c:\users\user\desktop\project source\ocgcore\duel.cpp 108 1 ocgcore
(Visual Studio 2010)
MS VC++ 2010 does not support the range based for statement that was introduced in the C++ 2011. However it has its own language extension: for each.
Try to change this code
void duel::restore_assumes() {
for(auto pcard : assumes)
pcard->assume_type = 0;
assumes.clear();
}
to
void duel::restore_assumes() {
for each (auto pcard in assumes)
pcard->assume_type = 0;
assumes.clear();
}
Otherwise you can use an ordinary loop with iterators of object assumes or some standard algorithm as for example std::for_each
As shown in this table : C++11 Compiler Support
Range-based for Loops aren't available with MSVC2010, but with MSVC2012 (which is version 11).
So if you use the 2010 compiler, this code won't compile.
The error message makes it pretty obvious: the compiler is not expecting a : in the for statement.