I know that arrays have to be given a size when using them, so I tried to, but this piece of code doesn't seem to want to be accepted by the compiler.

data is a std::vector<char> so i tried using the .size() function to use the size of the vector to initialize the array. Maybe there is a different way to go about this, but i want to take each char in the array and sore it into the array.

int fileTempSize = data.size();
char fileTemp[fileTempSize];

Recommended Answers

All 4 Replies

C++ 98 does not accept variable sized arrays. You must define the size of an array at compile time. If you cannot know the size at compile time, you will have to allocate memory yourself, or alternatively use one of the C++ STL container classes.

The size of an array must be a constant known at compile time. You should dynamically allocate it char * fileTemp = new char[fileTempsize]; instead. Some compilers are able to do what you tried to do, but it's an extension and is non-standard.

May have to dynamically allocate the memory using "new" and "delete".

Also, in this line:

int fileTempSize = data.size();

int doesn't have the range of the unsigned int, make sure your fileTempSize is the same type as the type returned by data.size()

this can cause problems if the size is greater than std::numeric_limits<int>::max(), it will overflow the variable.

std::vector<char>::size_type fileTempSize = data.size();

Or if you have a compiler that supports the new "auto" keyword..

auto fileTempSize = data.size();

Meh i feel so dumb, was having a blank spot there, forgot i can allocate the memory xD Ty.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.