non-static data member initializers only available with -std=c++11 or -std=gnu++11..what does this mean? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

non-static data member initializers only available with -std=c++11 or -std=gnu++11..what does this mean?

I've initialized the variable in private assessor and then used them in public..Although the program ran successfully but the compiler is giving this warning..what does this mean..couldn't understand the complex explanation on stackoverflow

2nd Jul 2018, 1:32 PM
Maleeha Khalid
Maleeha Khalid  - avatar
8 Answers
+ 1
#include <iostream> using namespace std; class operations{ public : operations(): x{4},y{2} {} int sum,sub; void op(){ int a,b; a=x; b=y; sum=a+b; sub=a-b; cout<<sum<<endl<<sub; } private : int x,y; }; int main() { operations op; op.op(); return 0; }
2nd Jul 2018, 2:08 PM
MO ELomari
+ 1
-std=c++11 is the compilation macro for c++11. when you have c++11 available on your setup and if you want to compile a code for c++11 you can use that macro. gcc exam.cpp -std=c++11
2nd Jul 2018, 2:34 PM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar
+ 1
you can't initialize a member in the class definition unless it's static, const, so you should either initialize the x and y member through a constructor initializer list, or enable C++11 using the flags -std=c++11 or -std=gnu++11
2nd Jul 2018, 2:49 PM
MO ELomari
+ 1
Iterator Before C++11, the available ways to initialize non-static data members* were using class' constructor as the initializer like class A { public: A() { a = 10; } private: int a; }; or class A { public: A(int n) : a(n) {} private: int a; }; int main() { A obj(10); } After C++11, another method of doing so also added which was somehow more natural to the way we are initializing local variables. And that what you have used in your class. So, your particular compiler issued a warning** regarding to availability of the later method of initialization in C++11 and above. _____ * Those data members that don't bound to the class instances which means they are shared between all instances. ** For C++98/03 it will issue an error instead.
2nd Jul 2018, 3:40 PM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
0
could you share your code?
2nd Jul 2018, 1:47 PM
MO ELomari
0
It is not giving any warning here on this app but whenever i run it on laptop dev c++ , it shows warning although it runs successfully. https://code.sololearn.com/cV0AnZ2muvx4/?ref=app
2nd Jul 2018, 1:57 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
Mohammed Elomari
2nd Jul 2018, 1:57 PM
Maleeha Khalid
Maleeha Khalid  - avatar
0
can you explain the reason also? why should we do it? Mohamed Elomari
2nd Jul 2018, 2:13 PM
Maleeha Khalid
Maleeha Khalid  - avatar