Unexpected error on declaring vector in c++ - 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

Related

List initialization returns semicolon error

I'm trying to compile the following C++ code on Visual Studio Code, using the Mac clang compiler.
#include <iostream>
int main() {
int x { 5 };
std::cout << x;
return 0;
}
However, this returns an error, on the line of the list initialization: int x{ 5 };. Specifically, it says I need to insert a semicolon after the x.
I don't get what's wrong with this code, it works fine on an online compiler. How do I fix this?
Running man clang in the Terminal and skimming through, I found this:
The default C++ language standard is gnu++14.
UPDATE: I ran clang++ main.cpp in the compiler and it returned that semicolon error. This isn't a problem with VSCode, so I'll remove that tag.
Here's the error:
main.cpp:3:10: error: expected ';' at end of declaration
int x { 5 };
^
;
1 error generated.

(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

Stoi was not declared in scope - Code::blocks

Edit: I'm trying to tell it to work with C++11 by clicking "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.
I'm getting stoi was not declared in scope, and I've added c++11 to Code::Blocks; I've added compatibility in Settings -> Compilers -> Compiler flags, but it still keeps giving me that error.
And when I try to do atoi or strtol I get the following error:
C:\Users\user\Desktop\Programming\NewProject\main.cpp|19|error: cannot
convert 'std::string {aka std::basic_string}' to 'const char*'
for argument '1' to 'long int strtol(const char*, char**, int)'|
My code:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
string numberGuessed;
int numberGuessedint = 0;
do {
cout << "Guess a number between 1 and 10: ";
getline(cin, numberGuessed);
numberGuessedint = stoi(numberGuessed);
cout << numberGuessedint << endl;
} while(numberGuessedint != 4);
cout << "You win!" << endl;
return 0;
}
It is a known bug in MinGW bundled with Code::Blocks.
You can apply a patch: http://tehsausage.com/mingw-to-string
Or download fresh version of MinGW (preferable with threading support, as you lack it too) and replace one you have right now.
To use atoi you need:
numberGuessedint = atoi(numberGuessed.c_str());
I am writing a solution which worked for me. As I found in most of the solutions posted on stack overflow, code blocks earlier versions contain a bug. So I deleted my older code blocks version and installed a new version 17.12 from code blocks website.
Then I just clicked on "Have g++ follow the C++11 ISO C++ language standard" in the compiler flags.
Settings -> Compilers -> Compiler flags.
It works for me(I am using windows 7).

Using auto in loop error "auto must have an initializer"

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

Range For loop help: Expected initializer before ":" token

I am completely new to C++ and I am now following the C++ Primer book.
I wrote a small example about strings, here is the code:
#include <iostream>
#include <string>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main() {
string s("Hello World");
for (auto &c : s)
c = toupper(c);
cout << s << endl;
return 0;
}
I am on Linux with GCC version 4.4.6 and I tried to compile this code with:
g++ test_strings.c -std=c++0x
but got the following errors:
test_strings.c: In function 'int main()':
test_strings.c:14: error: expected initializer before ':' token
test_strings.c:19: error: expected primary-expression before 'return'
test_strings.c:19: error: expected ')' before 'return'
I copied the program from the textbook, so I though it was a misspelling but after a check and trying searching on the web and updating my gcc the error reminds. Help will be greatly appreciated, thanks in advance.
As per the C++0x/C++11 Support in GCC page, you need to be running gcc 4.6 to get the range-for feature.
The 4.6 changes page contains:
Improved experimental support for the upcoming C++0x ISO C++ standard, including support for constexpr (thanks to Gabriel Dos Reis and Jason Merrill), nullptr (thanks to Magnus Fromreide), noexcept, unrestricted unions, range-based for loops (thanks to Rodrigo Rivas Costa), opaque enum declarations (thanks also to Rodrigo), implicitly deleted functions and implicit move constructors.
Since you're running gcc 4.4.6, it's not available to you.