Getting error in initialising the vector in C++ [duplicate] - c++

When I try to initialize a vector of ints, I always get this error:
expected ';' at end of declaration
I used the original code from "C++ Primer":
vector<int> v{1,2,3,4,5,6,7,8,9};
and
$ g++ -o test test.cpp
I think this is a silly question to ask, but I am sure that there is a ;. I cannot manage to find an answer.

g++ assumes C++03 by default, and the syntax you're trying to use came in C++11. Change the compilation line to:
$ g++ -std=c++11 -o test test.cpp
Or, as I'd personally prefer:
$ g++ -Wall -Werror -pedantic -std=c++1y -o test test.cpp
:)
Note: whether you'd use c++0x, c++11, or c++1y (and possibly c++14) depends mostly on the compiler version, as those were introduced in succesion.

Your compiler by default does not support brace initialisation; this was added in C++11.
There's probably a command line argument you can use in your compiler, something along the lines of
-std=c++11

Related

Mutex and condition_variable compiling error with g++ [duplicate]

I'm trying to update my C++ compiler to C++11.
I have searched a bit and I have come to the conclusion that I have to use the flag -std=c++0x or -std=gnu++0x, but I don't know many things about flags. Can anyone help me? (I'm using Ubuntu 12.04.)
Here is the error that I get from the compiler when I attempt to use a library which is included in C++11 (i.e. array):
#include <array>
#include <iostream>
int main()
{
std::array<int, 3> arr = {2, 3, 5};
...
}
This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable.
Assuming you are invoking g++ from the command line (terminal):
$ g++ -std=c++11 your_file.cpp -o your_program
or
$ g++ -std=c++0x your_file.cpp -o your_program
if the above doesn't work.
You can check your g++ by command:
which g++
g++ --version
this will tell you which complier is currently it is pointing.
To switch to g++ 4.7 (assuming that you have installed it in your machine),run:
sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc).
Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/gcc-4.6 60 auto mode
1 /usr/bin/gcc-4.6 60 manual mode
* 2 /usr/bin/gcc-4.7 40 manual mode
Then select 2 as selection(My machine already pointing to g++ 4.7,so the *)
Once you switch the complier then again run g++ --version to check the switching has happened correctly.
Now compile your program with
g++ -std=c++11 your_file.cpp -o main
You can refer to following link to know which features are supported in which version of compiler. It has an exhaustive list of feature support in modern compilers. Seems like GCC follows the standard very closely and implements before any other compiler.
Regarding your question, you can compile using
g++ source_file.cpp -o executable_name -std=c++11 for C++11
g++ source_file.cpp -o executable_name -std=c++14 for C++14
g++ source_file.cpp -o executable_name -std=c++17 for C++17
g++ source_file.cpp -o executable_name -std=c++2a for C++20, All the features of C++20 are not yet supported. Refer to this link for feature support list in GCC.
The list changes pretty fast, keep an eye on the list, if you are waiting for a particular feature to be supported.
Your Ubuntu definitely has a sufficiently recent version of g++. The flag to use is -std=c++0x.
If you want to keep the GNU compiler extensions, use -std=gnu++0x rather than -std=c++0x. Here's a quote from the man page:
The compiler can accept several base standards, such as c89 or c++98,
and GNU dialects of those standards, such as gnu89 or gnu++98. By
specifying a base standard, the compiler will accept all programs
following that standard and those using GNU extensions that do not
contradict it. For example, -std=c89 turns off certain features of GCC
that are incompatible with ISO C90, such as the "asm" and "typeof"
keywords, but not other GNU extensions that do not have a meaning in
ISO C90, such as omitting the middle term of a "?:" expression. On the
other hand, by specifying a GNU dialect of a standard, all features
the compiler support are enabled, even when those features change the
meaning of the base standard and some strict-conforming programs may
be rejected. The particular standard is used by -pedantic to identify
which features are GNU extensions given that version of the standard.
For example-std=gnu89 -pedantic would warn about C++ style //
comments, while -std=gnu99 -pedantic would not.
Use -std=c++11 compiler flag for ISO C++11.
For more details on C++ compiler flags and options, check this.

Why const keyword without identifier in function parameter compiles?

As I was playing with the C++ syntax, I coded something I thought wouldn't compile.
The following code is simplified to isolate the problem:
void foo(const) {} //why doesn't the compiler flag this line as error?
int main() {}
I compiled the above source code for C++ on MinGW's g++ compiler, and it did so without any warning or error.
Out of curiosity, I would like to know if there is any intention behind why const is allowed without a following identifier inside a function parameter.
Or is this perhaps a bug?
EDIT: Take a look at this and witness the mystery yourself.
You are probably running your compiler without enabling useful warnings. Many compilers emit only the diagnostics that the Standard requires them to, unless you specifically ask for more:
g++ -std=c++17 -fPIC -g -Wall -Wextra -Wwrite-strings -Wno-parentheses -Wpedantic -Warray-bounds -Weffc++ -c -o 16019202.o 16019202.cpp
16019202.cpp:1:10: error: ISO C++ forbids declaration of ‘parameter’ with no type [-fpermissive]
void foo(const) {} //why doesn't the compiler flag this line as error?
^~~~~
(That's with GCC 6.3.0 on Debian).

expected ';' at end of declaration /vector /c++

When I try to initialize a vector of ints, I always get this error:
expected ';' at end of declaration
I used the original code from "C++ Primer":
vector<int> v{1,2,3,4,5,6,7,8,9};
and
$ g++ -o test test.cpp
I think this is a silly question to ask, but I am sure that there is a ;. I cannot manage to find an answer.
g++ assumes C++03 by default, and the syntax you're trying to use came in C++11. Change the compilation line to:
$ g++ -std=c++11 -o test test.cpp
Or, as I'd personally prefer:
$ g++ -Wall -Werror -pedantic -std=c++1y -o test test.cpp
:)
Note: whether you'd use c++0x, c++11, or c++1y (and possibly c++14) depends mostly on the compiler version, as those were introduced in succesion.
Your compiler by default does not support brace initialisation; this was added in C++11.
There's probably a command line argument you can use in your compiler, something along the lines of
-std=c++11

gnu gcc How to suppress warning: ‘typedef’ was ignored in this declaration [enabled by default]

I am using GNU gcc 4.6.2 on Fedora 16. I am writing an application using a 3rd party API, after compilation, I got a lot warnings.
warning: ‘typedef’ was ignored in this declaration [enabled by default]
Just wondering how can I suppress this? I compile my program with -Wall flag.
In this doc, http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html, it mentioned something like -Wunused-local-typedefs.
I have tried -Wno-unused-local-typedefs, but doesn't work.
Thanks.
-Wno-unused-local-typedefs works in GCC 4.8.
gcc allows you to specify that certain library include paths should be treated as system libraries with the -isystem switch which allows those headers special treatment with respect to the flags you use on the rest of your code. So for example if you have unused local typedefs from using certain Boost libraries in test.cpp (I ran into this using including the Boost signals2 library recently)
g++ -o test{,.cpp} -Wall -Wextra -Werror -I /usr/local/boost-1.55.0/include -L /usr/local/boost-1.55.0/lib
and the above does not build cleanly try the following
g++ -o test{,.cpp} -Wall -Wextra -Werror -isystem /usr/local/boost-1.55.0/include -L /usr/local/boost-1.55.0/lib
which will (provided the warnings coming from the Boost libraries you are including in test.cpp are your only problem of course).
According to the gcc-source-code(gcc/cp/decl.c:4108):
warning (0, "%<typedef%> was ignored in this declaration");
There is no command line flag(that is what the 0 stands for) to suppress this warning in gcc 4.6.2.
As -Wunused-local-typedefs is part of -Wall, make sure you don't have -Wall after -Wno-unused-local-typedefs. If you do, -Wall just turns the option back on again.
In C++17, you should use [[maybe_unused]].
For an overview of all attributes, please see http://en.cppreference.com/w/cpp/language/attributes.
Proposal: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0212r0.pdf
(sorry, I could't post an example, as it's considered as badly indented by stackoverflow)
This GCC warning means that your typedef maybe duplicated and you should remove typedef keyword instead. For example:
typedef enum class Something {
THING1,
THING2,
} Something;
This code above is type duplicate, because enum class is defined as type already. So you must remove typedef keyword as well as Something at the end too!

Why does my C++0x code fail to compile if I include the "-ansi" compiler option?

I've come across a really weird error that only pops up if I use the ansi flag.
#include <memory>
class Test
{
public:
explicit Test(std::shared_ptr<double> ptr) {}
};
Here's the compilation, tested with gcc 4.5.2 and 4.6.0 (20101127):
g++ -std=c++0x -Wall -pedantic -ansi test.cpp
test.cpp:6:34: error: expected ')' before '<' token
But compiling without -ansi works. Why?
For the GNU C++ compiler, -ansi is another name for -std=c++98, which overrides the -std=c++0x you had earlier on the command line. You probably want just
$ g++ -std=c++0x -Wall minimal.cpp
(-pedantic is on by default for C++, so it's unnecessary to say it again. If you want pickier warnings, try adding -Wextra.)
std::shared_ptr doesn't exist in c++98. Try these changes:
#include <tr1/memory>
...
explicit Test(std::tr1::shared_ptr<double> ptr) {}
Um, because there is not yet an ANSI standard for C++0x? The ANSI flag checks for conformance with existing standards, not future ones.