Unexpected result when initializing variable in C++ using curly braces - c++

I'm using atom to practice C++ (I'm very new). I just learned to initialize variables like the following:
#include <iostream>
using namespace std;
int main() {
int myInt {};
return 0;
}
When I build and run the previous code in codelite I receive no errors. However, if I compile my atom file dailyPractice10.cpp using my MacBook terminal (zsh) I get the following error:
dailyPractice10.cpp:7:12: error: expected ';' at end of declaration
int myInt {};
^
;
1 error generated.
I'm using the following command to compile it on terminal:
g++ -o dailyPractice10 dailyPractice10.cpp (compiles)
./dailyPractice10 (runs program)
Does anyone have any feedback why this code runs in codelite but doesn't compile in terminal?

Because this feature is added from c++11.
if you will like to try below command.it will work.
$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp

The key to fixing this issue is to set the C++11 (or above) standards while building your code.
In the console tab of the IDE, the following output is generated before the error. Notice that no standard is being defined while building the code:
make all
Building file: ../1.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"1.d" -MT"1.o" -o "1.o" "../1.cpp"
We need to add the --std=c++1x flag to the g++ command. The following solution is for the ones using the Eclipse IDE and the MacOSX C++ compiler:
Right click on the project from the "Project Explorer".
Go to Properties > C/C++ Build > Settings.
Under the "Tool Settings" tab, find "GCC C++ Compiler" > "Miscellaneous"
In the "Other Flags" text box, edit the text such that it looks like:
-std=c++17 -c -fmessage-length=0
If you intend to use any other c++ standard, replace "c++17" with the standard of your choice ( eg. c++20).
Apply Changes.
Run Clean, and the Build again.

you should try this to compile the Code
g++ -std=c++20 -o dailyPractice10 dailyPractice10.cpp

Related

In Visual Studio Code: "no template named 'initializer_list' in namespace 'std'"? [duplicate]

I wanted to compile C++11 source code within Mac Terminal but failed. I tried g++ -std=c++11, g++ -std=c++0x, g++ -std=gnu++11 and g++ -std=gnu++0x but nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnu and things like that worked fine (of course C++11 source code could not pass).
Which option should I use to turn on C++11 support?
By the way, the command line tool I'm using is installed within Xcode, and I'm pretty sure that they are up-to-date.
As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.
clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.
xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.
For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.
XCode uses clang and clang++ when compiling, not g++ (assuming you haven't customized things). Instead, try:
$ cat t.cpp
#include <iostream>
int main()
{
int* p = nullptr;
std::cout << p << std::endl;
}
$ clang++ -std=c++11 -stdlib=libc++ t.cpp
$ ./a.out
0x0
Thanks to bames53's answer for pointing out that I had left out -stdlib=libc++.
If you want to use some GNU extensions (and also use C++11), you can use -std=gnu++11 instead of -std=c++11, which will turn on C++11 mode and also keep GNU extensions enabled.

Curly brace uniform initilaization not recognised in Eclipse in c++

I am learning C++ and I am using Eclipse as an IDE. I downloaded the latest version for Mac (Version: 2018-12 (4.10.0)). I am trying uniform initialization but Eclipse gives me an error.
Here is the code:
#include <iostream>
int main()
{
int x{ 5 };
std::cout << x;
return 0;
}
If I type instead:
int x(5);
or
int x=5;
It works!
The error shown is the following:
Building file: ../src/HelloWorld2.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/HelloWorld2.d" -MT"src/HelloWorld2.o" -o "src/HelloWorld2.o" "../src/HelloWorld2.cpp"
../src/HelloWorld2.cpp:5:10: error: expected ';' at end of declaration
Any ideas?
I couldn't find how to make Eclipse compatible for C++11, but then I found a website (took a while). Here is the solution.
On the project explorer window, right-click your project and choose properties at the bottom of the menu.
Go to "C/C++ Build" on the left, expand the options by clicking the small arrow and then select 'settings'
On the right side of the window, there is a list of settings. Select the 'Miscellaneous' one.
There will be a field called 'Other flags' to the right of that list. Add the following (put a space at the end of the current text).
"-std=c++0x"
That's it!

Error: Range-based 'for' loops are not allowed in C++98 mode

So I'm following the tutorials on this page:
http://www.cplusplus.com/doc/tutorial/control/
But I'm having trouble doing a range/based for loop. I found this page:
GNU GCC compiler updatingThe answer there says I should open "Project" and "Properties". But when I try that, the "Properties" option is grayed out with no explanation:
http://imageshack.com/a/img571/4371/xd1x.png
So.. how can I activate range/based for loops?
Pass -std=c++11 flag to the compiler. Certainly GCC should be fresh enough (>=4.7) to support all these modern standards. For CodeBlocks 13.12: Settings -> Compiler -> Tab "Compiler Flags" -> Option "Have g++ follow the C++11 ISO C++ [-std=c++11]"
The above given solution of using -std=c++11 didn't work for me.
This is the target and version detail of my compiler.
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
Target: x86_64-linux-gnu
When I tried, this is what happened.
$ g++ -std=c++11 program.cpp
cc1plus: error: unrecognized command line option ‘-std=c++11’
This solved the problem for me.
$ g++ -std=c++0x program.cpp
In Dev-Cpp 5.11 Simply you can click on Tools>Compiler Options>Settings>Code Generation>(and in last option)Language standard(-std) select from dropdown (ISO C++ 11).
If you faced this error in Code::Blocks, this might help you -
Click on Settings -> Compiler -> Compiler Settings -> Compiler Flags
Under the "General" section, check [✓] the box beside :
Have g++ follow the C++11 ISO C++ language standard [-std=c++11]
Both of these:
g++ -std=c++11 -o test_executable test_source.cpp
g++ -std=c++0x -o program program.cpp
worked for me.
Only thing to do after compiling is to execute the test_executable (in the first case) as ./test_executables or program (in the second case) as ./program.
Using the above solution
g++ -std=c++0x program.cpp
works. However, the command needs to be modified slightly in order to run the program with the common command:
./program
I used
g++ -std=c++0x -o program program.cpp
and all worked perfectly.
If you are using QT5.5, you can achieve it by adding following lines in your .pro file.
CONFIG += c++11
The best solution is doing this, in Dev C++:
Go to the "Tools" Option
Select "Compiler Options"
Click "Settings"
Click "Code Generation"
Go to "Choose Language" and select "ISO C++11"
Then your problem is resolved.
in first time, if you have Dev-C++
C:\Program Files (x86)\Dev-Cpp\MinGW64\bin, you must add this route to the path
then use the next command in cmd.
g++ -std=c++11 -o outprogram code_source.cpp
this command in your project directory.

Configuring g++ from Code::Blocks doesn't take affect on command line

I'm trying to change the settings of g++ from the Code::Blocks IDE. I went to the Settings tab, clicked Compiler... and checked various options for the compiler to use, like
Enable all warnings (-Wall)
Have g++ follow the C++11 ISO C++ language standard (-std=c++11)
......
These are just two of many others; when I compile on the command line, here is what comes up:
g++ -o example example.cpp
# warning: initializer lists only available with -std=c++11 ...
Notice how there's no warning either - I have an unused variable in my program. It only works if I give the options manually:
g++ -Wall -std=c++11 -o example example.cpp
Do you think I might have done something wrong when setting up the compiler? Why aren't the options taking affect?
Invoking the compiler from the ide is completely independent from doing it in a command line shell. There's no reason for the setting and usage of one to have any effect on the other.

GoogleTest 1.6 with Cygwin 1.7 compile error: 'fileno' was not declared in this scope

GoogleTest 1.6 with Cygwin 1.7: 'fileno' was not declared in this scope
Error message when building a simple test on Factorial() function in Eclipse CDT:
Invoking: Cygwin C++ Compiler
g++ -std=c++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
In file included from E:\source\gtest-1.6.0\include/gtest/internal/gtest-internal.h:40:0,
from E:\source\gtest-1.6.0\include/gtest/gtest.h:57,
from ../src/challenge.cpp:11:
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1589:51: error: 'fileno' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1595:57: error: 'strdup' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1627:71: error: 'fdopen' was not declared in this scope
Eclipse CDT 8.1 running gcc 4.7.3 on Cygwin 1.7.22
gTest 1.6 succesfully built including demo tests, with cmake 2.8.9 on Cygwin 1.7.22
I've linked the built lib with full path, E:\lib\gtest-1.6.0\Cygwin\libgtest.a
The following command option was added manually, got same error without it.
-DGTEST_OS_CYGWIN=1
Seems the errors have nothing to do with my code. Anyone using gTest with Eclipse and Cygwin?
Thank you,
unsigned long Factorial(unsigned n) {
return n==0? 0 : n*Factorial(n-1);
}
// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
EXPECT_EQ(1, Factorial(0));
}
// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}
Setting the C++ standard to -std=gnu++0x rather than -std=c++0x, worked for me. You can try the statement:
g++ -std=gnu++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
Setting symbol (-DGTEST_OS_CYGWIN=1) has got nothing to do with this error.
Some functions go beyond the ANSI standard.
These are disabled when you use std=c++11 (or std=c++0x).
Among them are fdopen, fileno and strdup.
There are two possibilities to use them:
Use the GNU dialect (std=gnu++11).
If you want to compile without dialect and make a local exception, you can include stdio.h with the __STRICT_ANSI__ undefined. (see: Error "'fdopen' was not declared" found with g++ 4 that compiled with g++3)
I have tested both on Suse Linux Enterprise 11, MinGW and Cygwin.
Addition: Another (possibly better) way to access non-ANSI symbols would be to add
#define _POSIX_C_SOURCE 200809L
before the first #include in your file. This will give you access to most of the non-standard routines.
Some functions (e.g. realpath(...)) require
#define _BSD_SOURCE
to be inserted on top of your file.