expected ';' at end of declaration /vector /c++ - 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

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

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

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

g++ (MinGW), C++11 and SSE

When I try to compile the next simple example of code:
echo "#include <cmath>" | g++ -x c++ -c - -m64 -mfpmath=both -std=gnu++11 -o /dev/null
(along with -m64 option is activated (by default) a using of SSE (say, AVX)).
It is appear the following error message:
In file included from <stdin>:1:0:
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/cmath:1040:11: error: '::double_t' has not been declared
c:\mingw64\bin\../lib/gcc/x86_64-w64-mingw32/4.7.2/../../../../include/c++/4.7.2/cmath:1041:11: error: '::float_t' has not been declared
Is this a bug?
Without -mfpmath=both option an error does not happen.
My workaround is to add the -D__FLT_EVAL_METHOD__=2 (and -Wp,-w if -Werror is present, because of "is redefined" warning) to g++'s option list. I think that this is a dirty way.
This error is in wrong commit on mingw-w64 trunk. I wrote a bug report for this. Thanks!

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.