| metulburr (278) | |||
i thought this was correct, but i get an error saying:main.cpp:14:12: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]But by assigning variable in the constructor as ints, not array works? All the examplse i find, are just init and defin array, not in a class. But if you cannot define a member in the class, then how to you define it in the constructor?
EDIT: Really? I just found a forum where someone said you have to define each index separately or using a for loop? Is this accurate? | |||
|
Last edited on
|
|||
| NwN (770) | ||
|
Hi there, You need to read the error more carefully. It says it is possible, but only using the new standard C++11 (also called c++0x). You need to compile using the "std" flag:
As far as I recall, indeed you used to have to initialise arrays using a for loop. If you're going to use C++11 anyway - consider using std::array instead of the old style arrays, they provide more protection against buffer overflows.Hope that helps. :) All the best, NwN | ||
|
|
||
| JLBorges (1756) | |||
This is assignment: Test::Test(){ pos = {0,0}; /* *** error *** */ }This is initialization: Test::Test() : pos{0,0} /* fine */ {}
| |||
|
Last edited on
|
|||
| metulburr (278) | |
|
@MwN i wasnt trying to do it the new C++11, but C++98. @JLBorges im not sure i understand your code. I'll have to research it. | |
|
Last edited on
|
|
| NwN (770) | |
|
@JLBorges Thanks very much for that, I was unaware that initializer lists could be used in this way - although the name actually kind of gives it away, I only thought of it in the context of the uniformity. @metulburr Yes that is still C++11 code, I think in C++98 you are stuck with the for-loop solution. All the best, NwN | |
|
|
|
| metulburr (278) | |
|
Kind of a noob question but: Does it matter which you compile any code as c++11, or c++98, will it run in the end on other computers? I still dont quite understand the the process from code to open source to others being able to compile the programs also. Isn't c++11 still in testing? | |
|
Last edited on
|
|
| NwN (770) | |
|
C++11 is standard now, the 11 stands for 2011, when it became the standard. Compiling C++ code could be the most easily explained by saying that the compiler actually translates your C++ code into machine code, which is code which the CPU understands and runs. Once "translated", this code will run on any processor supporting the same machine code. This is why you'll find different downloads for the same programs for different processor architectures (a linux distribution for instance). These processors support different machine code languages (called instruction sets), so the C++ code needs to be translated to each of those different languages. It's kind of like making a website which you want to be understood by many languages. It's not good enough to translate it to one language, because still not everyone will understand it. Hope that clears your doubt. All the best, NwN | |
|
|
|
| metulburr (278) | |
| that clears up a lot. Thanks NwM. | |
|
|
|