1

I have the following code:

#include <iostream>

using namespace std;

int main()
{
   unsigned int endx = 5;
   unsigned int endy = 5;
   unsigned int endz = 5;

   int Matrix[endx+1][endy+1][endz+1] = {};

   return 0;
}

I get

error C2057: expected constant expression

Ok, how can I create the Matrix like shown in the code without vectors or dynamic allocated array?

2 Answers 2

2

As the error says, you need constant expressions to define array/matrix sizes; in that example, make integers const and it will compile.

const unsigned int endx = 5;
const unsigned int endy = 5;
const unsigned int endz = 5;

If you don't want dynamically allocated arrays or vectors, then you'll have to know the exact array size beforehand.

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

Comments

2

Arrays require constants when creating them. In this code, just making your end* variables const unsigned int should do it.

2 Comments

he do plus one: endx+1
Yes, if endx is type const unsigned int then endx + 1 is still a constant. It works...try it.

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.