error C2143: syntax error : missing ',' before ':' - c++

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.

Related

Using c++17 'structured bindings' feature in visual studio 2017

I'm trying to utilize some c++17 features such as structured bindings in my code but the compiler keeps giving me errors and I'm not sure if it's because I'm doing things wrong or if I haven't setup c++17 properly to work in VS17. The simple code I'm trying to compile is this:
#include <iostream>
struct S
{
int i = 0;
float f = 32.0f;
};
int main()
{
S s;
auto [i, f] = s();
std::cin.get();
return 0;
}
From the understanding of this article, this is how I would use the new c++17 syntax to return multiple values. However, I keep getting these errors:
c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2059: syntax error: 'empty declaration'
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2143: syntax error: missing ';' before '['
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): warning C4467: usage of ATL attributes is deprecated
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2337: 'i': attribute not found
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2337: 'f': attribute not found
1>c:\users\jason\documents\visual studio 2017\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp(16): error C2059: syntax error: '='
I've also tried setting the compiler switch to std:/c++latest inside the properties of the project but still no dice. What I'm I doing wrong?
As #KerrekSB noted in the comments, auto [i, f] = s(); should be auto [i, f] = s;.
You said that you're using VS2017 but then indicated that your compiler version is 19.00.24218, which is actually VS2015 Update 3... VC++ did not support structured bindings until VS2017 Update 3 (compiler v19.11.25503) — you need to update (or fix your project settings and/or build environment so that the correct compiler is used).
Only did a quick look at the page you linked but seems like you should have it like this:
#include <iostream>
struct S
{
int i = 0;
float f = 32.0f;
};
int main()
{
S s(); <--- You were missing the ()
auto [i, f] = s();
std::cin.get();
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);

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);

Boost Unit Testing and Visual Studio 2005/Visual C++ and the BOOST_AUTO_TEST_SUITE(stringtest) namespace?

I'm reading this article on the Boost Unit Testing Framework.
However I'm having a bit of trouble with the first example, my guess is that they left something out (something that would be obvious to hardcore C++ coders) as IBM often does in their articles. Another possibility is that my Visual Studio 2005 C++ compiler is just too old for the example.
#include "stdafx.h"
#define BOOST_TEST_MODULE stringtest
#include <boost/test/unit_test.hpp>
//#include "mystring.h"
BOOST_AUTO_TEST_SUITE(stringtest) // name of the test suite is stringtest
BOOST_AUTO_TEST_CASE(test1)
{
/*
mystring s;
BOOST_CHECK(s.size() == 0);
*/
BOOST_CHECK(0 == 0);
}
BOOST_AUTO_TEST_CASE(test2)
{
/*
mystring s;
s.setbuffer("hello world");
BOOST_REQUIRE_EQUAL('h', s[0]); // basic test
*/
BOOST_CHECK(0 == 0);
}
BOOST_AUTO_TEST_SUITE_END()
To me the BOOST_AUTO_TEST_SUITE and BOOST_AUTO_TEST_CASE lines look a little suspect (especially since they don't have quotes around the arguments, and they are undeclared identifiers...but this probably means they are macros and I'm not certain I understand the concept or if that is available in VC++ 8.0)...
#ifdef _MYSTRING
#define _MYSTRING
class mystring {
char* buffer;
int length;
public:
void setbuffer(char* s) { buffer s = s; length = strlen(s); }
char& operator[ ] (const int index) { return buffer[index]; }
int size() {return length; }
}
#endif
Is there any reason why this code won't work?
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(7) : error C2065: 'stringtest' : undeclared identifier
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(9) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(9) : error C2146: syntax error : missing ';' before identifier 'BOOST_AUTO_TEST_CASE'
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(9) : error C2065: 'test1' : undeclared identifier
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(10) : error C2448: 'BOOST_AUTO_TEST_CASE' : function-style initializer appears to be a function definition
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(18) : error C2065: 'test2' : undeclared identifier
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(19) : error C2448: 'BOOST_AUTO_TEST_CASE' : function-style initializer appears to be a function definition
1>c:\users\andy\documents\visual studio 2005\projects\unittesttests\unittesttests\unittesttests.cpp(29) : fatal error C1004: unexpected end-of-file found
Looks correct to me. My Boost.Test code looks the same way. I'm running VS2008, but I know it works in 2005 as well.
Seems like your problem lies elsewhere.
If you use precompiled headers (and why do you do that in such a small test program?), shouldn't stdafx.h be included as the very first thing in the file?
And what is the first line for? You don't seem to use it, and _MYSTRING is a reserved name in C++ (everything that begins with underscore followed by a capital letter is off limits)