Array can't be initialized

1
2
3
4
5
int targetTerm;
...	// find targetTerm

const int arraySize = targetTerm + 1;
unsigned fib[arraySize] = {0, 1, 2}; // ignore fib[0] 

I encounter a compilation error, saying that variable-sized object 'fib' may not be initialized.
I don't want to initialize fib[1], fib[2] one-by-one.
How can I solve this problem?
Last edited on
You must give a type to it such as int or long.

Try:
unsigned int fib[arraySize] = {0, 1, 2};
Last edited on
Adding int or long results the same. :(
@ Nikko YL: in C++ you may not declare an array with a length unknown at compile-time.
Maybe you should use std::vector instead (and hopefully your compiler supports C++11 initializer lists).

http://www.cplusplus.com/reference/vector/vector/

With vectors, you do not need to worry about size, as they resize automatically when you add elements. You can still use std::vector::reserve() to hint at the ideal size.

1
2
3
4
5
6
7
8
9
10
#include <vector>

// ...

std::vector<int> fib = {0, 1, 2};

fib.reserve(arraySize);

fib.push_back(3);
fib.push_back(5); // etc. 


@ Stormboy: be advised that int is inferred from unsigned (and also from signed, and long).

unsigned == unsigned int
long == long int

and so on...

@ Catfish666: I can't do this.
in C++98 'fib' must be initialized by constructor, not by '{...}'
could not convert '{0, 1, 2}' from '<brace-enclosed initializer list>' to 'std::vector<int>'


I try the same code with C++11 online compiler and it succeeds.
Is C++11 a newer version of C++?
Where can I get it?
Last edited on
> Is C++11 a newer version of C++?

C++11 is the common name for the current International Standard for C++. And C++98 is the older, superseded version.


> Where can I get it?

Perhaps you already have it. Is your operating system Unix (or Unix clone) or Windows?

Windows xp
what compiler are u using ? try adding -std=c++11 or -std=c++0x when
compiling

eg :
g++ hello.c -std=c++11

and see the version by :
g++ --version
Last edited on
It compiles successfully when adding -std=c++0x
What is meant by -std=c++11 or -std=c++0x?

I'm using MinGW.
My version is g++ (GCC) 4.6.2
Last edited on
IDEs bundled with GCC 4.71

Code Blocks (codeblocks-12.11mingw-setup.exe) http://www.codeblocks.org/downloads/binaries

CodeLite http://downloads.codelite.org/

Dec-C++ http://sourceforge.net/projects/orwelldevcpp/


Standalone GCC 4.8.2

Cygwin http://www.cygwin.com/
> What is meant by -std=c++11 or -std=c++0x?

-std= c++11 conform to the current International Standard for C++
(versions of gcc released after C++11 was voted in)

-std= c++0x conform to the working draft of the upcoming C++ standard
(versions of gcc released before C++11 was voted in)
To add my favorite MinGW alternative as of now:
http://nuwen.net/mingw.html

Is C++11 a newer version of C++?

Yes, you can think of it like that. C++11 is the current standard of C++.

C++98 - from 1998, first standard
C++03 - from 2003, identical to C++98 (improved for compiler/library programmers)
C++0x - the "prototype" of C++11
C++11 - from 2011, major improvements over C++98
C++1y - the "prototype" of C++14
C++14 - planned for next year, minor improvements like C++03
C++17 - planned for 2017, major improvements like C++11

http://www.stroustrup.com/C++11FAQ.html#1x
Last edited on
Is there one for 32 bit windows?
Everything mentioned earlier can be used on your Windos XP box, except for the NUWEN MinGW build http://nuwen.net/mingw.html which is x64-native.
Thanks all for giving me so much information.
Topic archived. No new replies allowed.