array declare must have const value

I'm trying to create an bool array that is the size of a variable. When Using the code below i get the error "expression must have a constant value". What am I doing wrong?

        string theWord = " "
  	theWord = getWord(theWord);
	const int SIZE = theWord.length();
	bool rightAndWrong[SIZE];
The array size needs to be known at compile time.
If that isn't possible, there are three options:
• Make the array larger than required, so its limit will never be reached (may be awkward or impossible).
• Use dynamic allocation with new and delete (OK).
• Use a vector (easiest)
You should be using std::vector, and I agree with Chervil when he said it's the easiest option.

One benefit is that a vector resizes automatically when new elements are added. So you don't need to worry about setting a correct size for it.
Making the array larger would put unpredictable values at default right? I dont really want it to have to set each value of the array to 'false'. I've never seen vector used before, can you show me an example or modify my example?
If you don't need to do any resizing or adding/deleting elements of the array after you declare it, then you can just #include <vector> and write

vector<bool> rightAndWrong(SIZE);
instead of
bool rightAndWrong[SIZE]; // This is an error anyways .
Then you can use rightAndWrong exactly the same way as you would any other array.
(Although, if you do anything more than access the elements of your array in your code, you might have to make a few modifications to get it to work with vector.)

Note, though, that vector<bool> is actually a specialization of the "standard" vector container that optimizes for space efficiency. Along with that comes a bunch of weird quirks that make vector<bool> behave slightly different from the normal vector (and that really screw you over when you're least expecting it).

If you want to be safe, I would use deque<bool> instead (#include <deque> ).
deque still has much of the familiar array syntax (accessing elements with the [] operator), so you won't have to change any of your existing code (other than the declaration).
(However, deque isn't quite an array, so if your program relies on the fact that array elements are stored in contiguous locations in memory, this probably isn't the solution for you. Then again, if that's the case, then vector<bool> won't work either.)
Topic archived. No new replies allowed.