1

So I am having an issue with not being able to make a new array with a variable size (the variable is declared as const int)

Here is the code

#include <iostream>
#include <string>

int newArraySize;

int main ()
{
   std::cin >> newArraySize;

   int const fin = newArraySize;

   int deduped[fin]; // Here is the error
}

The error I'm getting is

Error: Expression must have a constant value

I tried casting as a constant but still no luck (same error)

int const fin = const_cast<int const&>(newArraySize);
int deduped[fin]; 
2
  • 2
    The problem is with newArraySize, not fin. newArraySize isn't const. Commented Mar 3, 2013 at 19:56
  • You have other errors too, like for (int n = 0; n=20; n++) a couple of times. And a nasty one, if (10 > temp > 110). But anyway, does deduped ever outgrow duper? If not, why don't you just make them the same size? Commented Mar 3, 2013 at 19:59

2 Answers 2

3

C++ has (confusingly) two forms of const. Your fin is one type, but the other type is required for something to be the size of an array. The other type is newly called constexpr, and used to be "a compile-time constant". You see, all arrays have to be a fixed size known to the compiler in C++. So it's not enough to make a variable const, the compiler has to be able to figure out the value as well. So either newArraySize must be a compile time constant expression, or, more likely, you'll have to use a dynamic array, preferably managed by std::vector.

std::vector<int> deduped(newArraySize);

If you can't use a vector, there's other (worse) options: std::unique_ptr<int[]>, managing, dynamic memory yourself with int* deduped=new[newArraySize](); and delete deduped;, or making a local array with a compile-time-constant maximum size (1000), and separately keeping track of how many elements you're actually using.

Sign up to request clarification or add additional context in comments.

2 Comments

The objectives for the project I am working on state that I have to use one-dimensional arrays for this task, unfortunately. I appreciate the insight and would probably use it had the objective not required the array.
@JacobThompson There are no variable-length arrays in C++. You need to accomplish your objective using something else. A constant-size array perhaps? You also may wish to clarify what exactly is meant by "array" in your assignment.
-1

newArraySize is not declared const. Do that and it'll work.

3 Comments

The issue here is that newArraySize isn't constant. That is why I assign it to the value fin. Before that I make a new array size based on duplicates of an array a user assigns.
@JacobThompson Then you really need to update your question. If you want a variable-length array, obviously the number of elements cannot be const...
If it isn't constant, then you can't make a statically sized array based on it. Use a std::vector, or even a pointer and new/delete..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.