Using auto in loop error "auto must have an initializer" - c++

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

Related

(Learning C++) Why is my compiler reporting errors when I try to make a tuple?

I'm new to c++ and have taken the liberty to learn it this summer after coming from a python background. I was watching a video about how to create and use tuples within c++ and it seemed to have worked for the YouTuber, however when I replicated his steps, my compiler had thrown some errors even though there was no distinct differences in the code
code:
#include <iostream>
#include <string>
#include <tuple>
int main() {
std::tuple <int, std::string> person(18, "Chris");
std::cout << std::get<1>(person) << std::endl;
return 0;
}
Errors:
❯ g++ -o main Tuples.cpp && ./main
Tuples.cpp:7:10: error: no member named 'tuple' in namespace 'std'
std::tuple <int, std::string> person(18, "Chris");
~~~~~^
Tuples.cpp:7:20: error: expected '(' for function-style cast or type construction
std::tuple <int, std::string> person(18, "Chris");
~~~^
Tuples.cpp:8:30: error: use of undeclared identifier 'person'
std::cout << std::get<1>(person) << std::endl;
^
3 errors generated.
Video for reference: https://www.youtube.com/watch?v=T9-agjKW4PQ&list=PLzMcBGfZo4-lmGC8VW0iu6qfMHjy7gLQ3&index=16

How to initialize a vector in c++ in visual studio code [duplicate]

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.

Unexpected error on declaring vector in c++

I'm getting an unexpectred error when I initialize a vector in the main.
I was expecting the following output:
0 1 2
I can't see why it's not working. I also writed the same code in another pc using the same compiler, and it works.
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vett = {0,1,2};
for (int i : vett) {
cout << i << " ";
}
return 0;
}
error: could not convert '{0, 1, 2}' from '<brace-enclosed initializer list>' to 'std::vector<int>'|
You need to compile with at least C++11. List initialization came with C++11.
-std=c++11
You are compiling with something older than C++11, it does not support initializer list constructor.
If you are using Code::Blocks follow these steps:
Settings -> compiler -> compiler flags -> select C++11 or above

C++11 support in Emscripten

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.

Problem with std::make_tuple in C++0x

Tying to compile the following program with Visual Studio 10, I get lot of compile errors:
#include "stdafx.h"
#include <tuple>
#include <string>
#include <map>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
typedef std::tuple<std::string, std::string> key_t;
typedef std::map<key_t, std::string> map_t;
map_t the_map;
auto k = std::make_tuple("one", "two");
the_map[k] = "the value";
auto q = std::make_tuple("one", "two");
auto i = the_map.find(q);
std::cout << i->second << std::endl;
return 0;
}
Error 1 error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'const key_t' to 'const std::basic_string<_Elem,_Traits,_Ax> &' c:\program files (x86)\microsoft visual studio 10.0\vc\include\tuple 127 1 tuple
Coming from the line:
std::cout << i->second << std::endl;
Strange thing is, as least from my point of view, if I change these lines:
auto k = std::make_tuple("one", "two");
the_map[k] = "the value";
to
the_map[std::make_tuple("one", "two")] = "p";
the program compiles. So my question is of course why? I guess it has something to do with make_tuple and move semantics - but I do not understand what..
Apparently the error comes in fact from the line the_map[k] = "the value";
When you use the [] operator on a map, the library tries to create a std::pair<Key,Value> object. In your case, this becomes std::pair<std::tuple<std::string,std::string>,std::string>.
However if you use an intermediate variable k, the constructor of std::pair which is called is: (copy-pasted from the standard lib)
_Pair_base(const _Ty1x& _Val1, _Ty2x&& _Val2)
: first(_Val1), second(_STD move(_Val2))
{ // construct from specified values
}
This constructor is trying to make a copy of your key_t. Unfortunatly, the tuple implementation of MSVC++ is bugged at the moment and the copy fails to compile (see also this: C++0x : are tuples of tuples allowed?)
I can diagnosize more, because this implementation is not only bugged but also very complicated.
Boost's tuples should work but don't have an < operator, so you can't use them.
The "best" solution for the moment is to write the_map.insert(std::make_pair(k, "the value"));
This looks like a bug in VS10, for some reason it's trying to cast the key type to the value type.
This simplified version also fails.
typedef std::map<std::tuple<int, int>, int> map_t;
map_t the_map;
map_t::key_type k = std::make_tuple(1,2);
the_map[k] = 3;
Produces the following:
error C2440: 'initializing' : cannot convert from 'const std::tr1::tuple<_Arg0,_Arg1>' to 'int'