I would like to compile a C++ code using Emscripten, where I use some C++11 features. Unfortunately I get an error:
index.cpp:13:18: error: expected expression
vv.push_back({1.5f, 2.f});
^
index.cpp:14:18: error: expected expression
vv.push_back({5.f, 0});
^
index.cpp:15:18: error: expected expression
vv.push_back({1, 1});
^
index.cpp:17:9: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
for(auto& item : vv) {
^
index.cpp:17:20: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
for(auto& item : vv) {
I can't understand, why I get this errors. The newest Emscripten and Clang versions are activated using emsdk.
The code is:
#include<iostream>
#include<vector>
struct AA {
float a;
float b;
};
int main() {
std::vector<AA> vv;
vv.push_back({1.5f, 2.f});
vv.push_back({5.f, 0});
vv.push_back({1, 1});
for(auto& item : vv) {
std::cout << item.a << ' ' << item.b << std::endl;
}
}
I even get a message: LLVM version appears incorrect (seeing "4.0", expected "3.7")
If it is true, it should wotk, because "Clang 3.3 and later implement all of the ISO C++ 2011 standard."
Suggestion: add -std=c++11 to your compiler options.
-Wc++11-extensions is a flag to add warnings, not to add C++11 support.
Related
This question already has answers here:
Visual Studio Code c++11 extension warning
(9 answers)
Visual Studio Code: code not running for C++11
(1 answer)
Closed 10 months ago.
I just set up visual studio code and made a very basic c++ program to test it:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
for (const auto &i : vec) {
cout << i << endl;
}
}
I'm not sure if I did anything wrong, but I get the following errors:
test.cpp:7:17: error: non-aggregate type 'vector<int>' cannot be initialized with an initializer list
vector<int> vec = {1, 2, 3, 4, 5};
^ ~~~~~~~~~~~~~~~
test.cpp:8:16: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
for (const auto &i : vec) {
^
test.cpp:8:24: warning: range-based for loop is a C++11 extension [-Wc++11-extensions]
for (const auto &i : vec) {
^
2 warnings and 1 error generated.
In cpp standard, it says its using c++20. I'm not sure what compiler I'm using, because in settings it looks like it is using clang, but when it runs, the description says:
cd "/Users/(my name)/Dropbox/Mac/vsCodeProjects/c:c++/" && g++ test.cpp -o test && "/Users/(my name)/Dropbox/Mac/vsCodeProjects/c:c++/"test
Because it says g++ there I'm not sure if it using clang after all, but I can't check the version of g++ because when I do it returns the clang version. I've tried everything I've seen and fiddled a lot with the tasks.json and the cpp standard json but nothing works. Any help is appreciated.
I have recently downloaded MinGW into my computer but on using certain containers and iterators like unordered_map and auto it shows an unexpected error.
my code is as follows :
#include <bits/stdc++.h>
#include<unordered_map>
using namespace std;
int main()
{
unordered_map<string, int> umap;
umap["GeeksforGeeks"] = 10;
umap["Practice"] = 20;
umap["Contribute"] = 30;
for (auto x : umap)
cout << x.first << " " << x.second << endl;
return 0;
}
it gives the following error :
C:\Users\naima\Documents\cpp>g++ -o try2 try2.cpp
In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/unordered_map:35:0,
from try2.cpp:2:
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
try2.cpp: In function 'int main()':
try2.cpp:9:5: error: 'unordered_map' was not declared in this scope
unordered_map<string, int> umap;
^
try2.cpp:9:25: error: expected primary-expression before ',' token
unordered_map<string, int> umap;
^
try2.cpp:9:27: error: expected primary-expression before 'int'
unordered_map<string, int> umap;
^
try2.cpp:11:5: error: 'umap' was not declared in this scope
umap["GeeksforGeeks"] = 10;
^
try2.cpp:15:15: error: 'x' does not name a type
for (auto x : umap)
^
try2.cpp:19:5: error: expected ';' before 'return'
return 0;
^
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ';' before 'return'
try2.cpp:19:5: error: expected primary-expression before 'return'
try2.cpp:19:5: error: expected ')' before 'return'
The compiler told you exactly what was wrong. It usually will.
This file requires compiler and library support for the ISO C++ 2011 standard. This
support is currently experimental, and must be enabled with the -std=c++11 or
-std=gnu++11 compiler options.
You just have to compile with the proper flag, -std=c++11. I don't know if you are version-matching against what graders use or what, but there are very few good reasons to be on a minGW compiler where support for an 8 year old standard is still considered experimental.
You can see that it works as expected here: https://godbolt.org/z/JQxL00
If you remove the -std=c++11 flag, it will fail to compile and give you the same error message.
You might also notice that I altered the includes to only include what I use. This results in a much faster compile time, smaller executable, and an easier to understand piece of code (Since it is plain to see what standard features are being used). You also avoid polluting your namespace.
I am using the MSVC2013 64bit compiler under Windows 10.
According to:
std::cout << arma::arma_version::as_string() << std::endl;
I have version 6.100.1 (Midnight Blue) of the Armadillio library.
I have C++11 enabled, for example
auto il = { 10, 20, 30 };
for(auto ele : il)
cout<<ele<<endl;
is working. Also the library is correctly added, as the following code runs:
vec v;
v<<10<<20<<30;
cout<<v;
But trying to use initializer lists for Armadillio fails.
vec v = { 1.0, 2.0, 3.0 };
causes the compile error:
error: C2440: 'initializing' : cannot convert from 'initializer-list' to 'arma::Col'
No constructor could take the source type, or constructor overload resolution was ambiguous
In the folder armadillo-6.100.1\include\armadillo_bits
there is a config file called config.hpp.
There you find a paragraph saying:
#if !defined(ARMA_USE_CXX11)
// #define ARMA_USE_CXX11
//// Uncomment the above line to forcefully enable use of C++11 features (eg. initialiser lists).
//// Note that ARMA_USE_CXX11 is automatically enabled when a C++11 compiler is detected.
#endif
So it looks like the MSVC2013 64bit is not detected as a C++11 compiler from Armadillio. So uncommenting the line
// #define ARMA_USE_CXX11
Solved my problem. Now this is working like charm:
vec v = { 1.0, 2.0, 3.0 };
cout<<v;
The documentation says that vec is a typedef for Col<double:
For convenience the following typedefs have been defined:
vec = colvec = Col< double >
If we look on the Col constructors, we will find the following constructor that accepts an initializer list:
#if defined(ARMA_USE_CXX11)
template<typename eT>
inline
Col<eT>::Col(const std::initializer_list<eT>& list)
{
<...>
}
So my guess would be that ARMA_USE_CXX11 is not defined, and thus this constructor is not accessible.
I want to use the auto keyword but get this error when compiling (Visual C++ Express 2010)
typedef std::list<int> MyList;
int main()
{
const int args[] = {0, 1, 2};
MyList myList(std::begin(args), std::end(args));
for(auto& value : myList)
{
std::cout << value << std::endl;
}
}
Output:
error C2143: syntax error : missing ',' before ':'
error C2530: 'value' : references must be initialized
error C3531: 'value': a symbol whose type contains 'auto' must have an initializer
The C++ compiler in VS2010 does support range-based for loops, but with a pre-standard syntax.
(And seriously, you can't expect a compiler finished about 2 years before the Standard to be fully compliant)
for each (int& value in myList)
{
std::cout << value << std::endl;
}
There are two issues in your code:
You need to explicitly include the headers for the functionality you use.
#include <list>
#include <iostream>
VS2010 does not support C++11 range-based "for" loops. The feature was implemented much later. See the following table: http://msdn.microsoft.com/en-us/library/hh567368.aspx
I have a problem when attempting to use std::map in clang-3.3 and clang-3.0 on Ubuntu 12.04:
#include <iostream>
#include <map>
#include <string>
class A
{
public:
#if 0 //clang compiles ok
typedef std::map<std::string,std::string> MapKeyValue_t;
void PrintMap(const MapKeyValue_t &my_map
= MapKeyValue_t())
#else // clang compiles fail
void PrintMap(const std::map<std::string,std::string> &my_map
= std::map<std::string,std::string>())
#endif
{
std::map<std::string,std::string>::const_iterator it;
for (it = my_map.begin(); it != my_map.end(); it++)
{
std::cout << it->first << " " << it->second << std::endl;
}
}
};
int main()
{
A a;
a.PrintMap();
return 0;
}
However, while the code compiles in both g++ and clang I keep getting these errors as output:
test.cpp:14:36: error: expected ')'
= std::map<std::string,std::string>())
^
test.cpp:13:15: note: to match this '('
void PrintMap(const std::map<std::string,std::string> &my_map
^
test.cpp:14:24: error: expected '>'
= std::map<std::string,std::string>())
^
test.cpp:28:13: error: too few arguments to function call, expected 2, have 0
a.PrintMap();
~~~~~~~~~~ ^
test.cpp:13:2: note: 'PrintMap' declared here
void PrintMap(const std::map<std::string,std::string> &my_map
^
3 errors generated.
The closest thing I could find that matches my problem is this topic: How to pass std::map as a default constructor parameter
But, I have no idea what's wrong. Hopefully, someone can shed some light on this, please.
Update:
void PrintMap(const std::map<std::string,std::string> &my_map
= (std::map<std::string,std::string>()))
is ok. Thanks.
I compiled and run it successfully in vs2012.
So I think it's compilers problem.
The other posters are correct, I think this is an instance of Bug 13657 which should be fixed in Clang 3.4.
As mentioned in the bug report and the C++ Standard Core Language Active Issues page linked from there (and as you mentioned in your update), you can work around the issue by adding parentheses to the default value as follows:
void PrintMap(const std::map<std::string,std::string> &my_map
= (std::map<std::string,std::string>()))