This question already has answers here:
Using const int as array size
(5 answers)
Closed 4 years ago.
Visual Studio is for some weird reason showing me this error and keep saying that count is not const int while trying to initialised array. Check image.
Error: expression must have a constant value
This is function call at main:
std::string fileName("shows.tv");
const int COUNT = 10;
Episode** episodes = loadEpisodesFromFile(fileName, COUNT);
and this is function declaration at header file:
Episode** loadEpisodesFromFile(std::string, const int);
I don't get it. Variable count is already declared as const int but it's not working.
Episode* episodes[count] is not valid because count is a parameter from the function
loadEpisodesFromFile which is unknown at compiling time...
you need to use an std::vector instead
std::vector<Episode*> episodes(count);
Related
This question already has answers here:
Can you allocate an array with something equivalent to make_shared?
(4 answers)
shared_ptr to an array : should it be used?
(2 answers)
Closed 8 days ago.
I am using a template called Temp, and I have a shared pointer defined like this
std::shared_ptr<Temp[]> ptr;
I use make shared like this
this->ptr = std::make_shared<Temp[x]>();
Where x is int value for the number of elements I want to store.
int x = this->GetSize();
I get this error:
error: no matching function for call to
‘make_shared<unsigned char [x]>()’
This question already has an answer here:
Array[n] vs Array[10] - Initializing array with variable vs numeric literal
(1 answer)
Closed 3 years ago.
So I have 2 dimensional array called char Screen[50][5];
When I declare it like this -> char Screen[50][5]; everything works.
But when I put variables in square brackets instead of numbers I get the error saying Screen isn't declared.
I've tried this method of declaring char[][] Screen = new char[ScreenWidth][ScreenHeight]; too
int ScreenWidth = 50;
int ScreenHeight = 5;
char Screen[ScreenWidth][ScreenHeight];
[Error] 'Screen' was not declared in this scope
That error is not related to you using variables for array sizes, however, this is not possible, C-Style array sizes have to be known at compiletime.
Your error is that you use Screen somewhere else, but this line errors, so Screen never gets defined.
Use the constexpr keyword for your variables, and it's possible
constexpr int ScreenWidth = 50;
constexpr int ScreenHeight = 5;
char Screen[ScreenWidth][ScreenHeight];
EDIT:
You might be able to mark it const instead of constexpr, the compiler will most likely optimize that, but you can't rely on that.
This question already has answers here:
How to create a variable size char array in VC++
(5 answers)
Closed 3 years ago.
int n;
cin >> n;
const int size = n;
int arr[size];
I'm getting a compiler error message "Expression must have a constant value". I'm using visual studio 2013. But the array size is a const int, whose value does not change. How am I getting a compiler error?
There is no relation for compilation error and visual studio version. Compilation erros occur when you are violating C++ concepts. Here, you're receiving array size as an argument from the user and it is dynamic value. In C++ you cannot create dynamically varying size arrays, instead static arrays is possible. Else you need to switch your data structure either to list or map, based on your requirement.
This question already has answers here:
Array size at run time without dynamic allocation is allowed? [duplicate]
(8 answers)
Closed 4 years ago.
Total c++ newbie here.
I have a problem, where I can not assign the number of character in a string to an array size, like so..😓
string outStr;
ifstream input("read.txt");
getline(input, outStr);
int const n = outStr.length();
int arr[n];
error msg --> expression must have a constant value. although i have declared the "const"
Thanks in advance✌.
C++ does not support Variable Length Arrays. Use a std::vector instead.
This question already has answers here:
sizeof an array passed as function argument [duplicate]
(3 answers)
Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?
(10 answers)
Closed 8 years ago.
int size(int arr1[])
{
int size1=sizeof(arr1)/sizeof(int);
cout<<size1<<endl;
return size1;
}
void main()
{
int b[]={1,2,3,4,5};
int size2 = size(b);
cout<<size2<<endl;
for (int i=0;i<size2;i++)
{
cout<<b[i];
}
}
I have put the b[] function into size() and check the size then return value.
however, it just return 1 as the answer.
Can anyone please help me to solve this.
A beginner of C++
sizeof(arr1) in the function returns the size of a pointer, not of the whole array.
That´s just how the language is.
You´ve to determine the array size without sizeof:
Either pass a second parameter with the number, or fill the array in a way
you can find the end because a certain value is there (and nowhere else)