So many of you maybe think that it is an answered question, but my situation is qiuet different //becouse i declare array IN the FUNCTION//. The matter is: i want to make a string splitter, which separates a string into chunks which will be part of an array. The basic source code is:
string separate(string str, int chunk, string mode = "res2end"){
int strlen = str.length();
int full = strlen / chunk;
int arr_size = full+1;
string result[arr_size]; //this is the 25th row
int modf = strlen % chunk;
for(unsigned i=0; i<full; i++){
int msr = i*chunk;
int sp = msr - chunk;
string subres = str.substr(msr, chunk);
result[i] = subres;
}
if(modf != 0){
int restm = strlen - (full * chunk);
result[full+1] = str.substr(restm, modf);
}
return result;
}
As you see, i tried to set the lenght of the array but nothing! There is an error message:
..\fc.h(25) : error C2057: expected constant expression
..\fc.h(25) : error C2466: cannot allocate an array of constant size 0
..\fc.h(25) : error C2133: 'result' : unknown size
So if anyone should share with me the solution i would be very pleased!
string result[arr_size];is not legal C++. Your compiler might support it, butarr_sizeneeds to be a compile time constant. Usestd::vector<std::string>as type forresultand as return type.