Error with range based loop in Qt - c++

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.

Related

Visual Studio 2013 Unnecessary Syntax Errors

EDIT: Thanks to some help, I see that I was just missing parentheses. Problem solved.
Recently Visual Studio has been giving me errors on the simplest things that are not actually wrong, and because of this I cannot run any program, and it is very frustrating. I have Visual Studio Professional 2013 with Update 5, running on Windows 10. Let me give an example.
When I write the program:
#include <iostream>
using namespace std;
int main
{
cout << "Hello World!" << endl;
return 0;
}
I get the errors:
main.cpp(6): error C2143: syntax error : missing '}' before ';'
main.cpp(8): error C2059: syntax error : 'return'
main.cpp(9): error C2059: syntax error : '}'
main.cpp(9): error C2143: syntax error : missing ';' before '}'
Also, on cout, IntelliSense gives me the error "no suitable conversion function from 'std::basic_ostream...' to 'int' exists". And on return it says "error: expected a declaration." That same error is also given on the final bracket.
Why am I getting all of these nonsense errors and how to I make them stop appearing so I can run a program?
(P.S. I have tried writing the program with and without "using namespace std" and nothing changes.)
You're missing parenetheses from main:
#include <iostream>
using namespace std;
int main()
^^
{
cout << "Hello World!" << endl;
return 0;
}

Syntax errors when passing initializer list as function argument for vector in VS12

I have a function that takes in a vector as an input argument:
void expandVector(std::vector<int> inputVector, std::vector<int>& outputVector);
When I call this function, I typically only have 1 element in the input vector, so I pass in an initializer list with 1 element.
std::vector<int> expandedVector;
expandVector({1337}, expandedVector);
This works fine with gcc 4.8.2 but I get these errors when I try to compile with Visual Studio 2012:
source.cpp(353) : error C2143: syntax error : missing ')' before '{'
source.cpp(353) : error C2660: 'expandVector' : function does not take 0 arguments
source.cpp(353) : error C2143: syntax error : missing ';' before '{'
source.cpp(353) : error C2143: syntax error : missing ';' before '}'
source.cpp(353) : error C2059: syntax error : ')'
When I check the MSDN documentation for vector::vector, it lists the constructor with initializer list, and furthermore, the example shows it being used.
vector<int> v8{ { 1, 2, 3, 4 } };
Even though I don't explicitly declare and name a vector (like v8 in the example), shouldn't I still be able to pass in an initializer list to a function expecting a vector?
Answering my own question, thanks to Columbo for pointing me in the right direction.
Visual Studio 2012 doesn't support initializer lists, see Support for C++11 Features (Modern C++) on MSDN.
Also, the page for vector::vector I originally referred to is for VS 2013. When I picked the correct VS 2012 version, I found that this constructor is not available.
The solution will be to replace
expandVector({1337}, expandedVector);
with
expandVector(std::vector<int>(1, 1337), expandedVector);

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.

VS2008 C++ "interface" as a parameter name fails to compile

As the title says, I'm getting a compiler error in a VS2008 C++ program. I'm not sure how better to describe my problem than in code. The following compiles unless I uncomment the TEST line.
#include <windows.h>
#include <iostream>
using namespace std;
//#define TEST //<-- uncomment for error
#ifdef TEST
void test(void* interface)
{
return;
}
#endif
int main()
{
cout << "Hello World" << endl;
system("PAUSE");
return(0);
}
When uncommented I get the following errors:
1>main.cpp(7) : error C2332: 'struct' : missing tag name
1>main.cpp(7) : error C2144: syntax error : '<unnamed-tag>' should be preceded by ')'
1>main.cpp(7) : error C2144: syntax error : '<unnamed-tag>' should be preceded by ';'
1>main.cpp(7) : error C2059: syntax error : ')'
1>main.cpp(8) : warning C4094: untagged 'struct' declared no symbols
1>main.cpp(8) : error C2143: syntax error : missing ';' before '{'
1>main.cpp(8) : error C2447: '{' : missing function header (old-style formal list?)
This is unmanaged code, so I'm not sure what the issue with the word interface is. Is there any way to get this code to compile as is, or do I have to change every instance of the term interface to something else?
Thanks!
If your code needs to include Windows.h then you should avoid using the name interface as it's reserved for the use that the Windows SDK has reserved for it (essentially it's a synonym for the keyword struct). There are probably hacks to work around that problem (you could #undef interface after including the SDK headers), but you should probably avoid using that identifier.
The word interface is reserved by MSVC++, as it is a non-standard keyword added by Microsoft Compiler, which is used to define interface in MSVC++.
So use a different name for the parameter, something like this:
#ifdef TEST
void test(void* test_interface)
{
return;
}
#endif