Hello everybody,
Please let me know how can I define arrays with dynamic dimensions BEFORE THE MAIN FUNCTION STARTS. I need to use that arrays in classes defined before the main function.
Long story short, I need to read dimensions of some arrays from a text file which may be different each time we run the program, and these values or dimensions are used in classes defined before the main function starts. I can use #define the dimensions, but they should be variables to be read from a text
For example, the following program works fine as my array "p= new (nothrow) float[a]; " is defined inside the main function, but I need it to be defined before main for many reasons like it is used in my class which is defined before main, and many functions as well. This is my problem.
What is wrong with the way you have done it? If there are classes that will need the array, then initialise those classes after you have initialised the array. You should update your post with more code in order for me to help more
Thank you for quick reply. Here is part of my program before main() that I have defined some constants (8 floats from inp to particlenumber) and works fine. The problem is I want these constants all variables to be read from a text file like test.txt instead of constants. I have used them in main and all defined functions. Please help me. I need to hand over the runtime to be able to work with different configuration set by text.txt.
# include <iostream>
# include <fstream>
# include <ostream>
# include <cmath>
# include <ctime>
# include <iomanip>
Thanks, I got many errors like:
1>nn.cpp(20): error C2365: 'inp' : redefinition; previous definition was 'function' for int inp = 64 ;
or:
1>nn.cpp(53): error C2057: expected constant expression for float inputs[inp]; in class
or:
error C2057: expected constant expression, and 1>nn.cpp(54): error C2229: class 'nodeclass' has an illegal zero-sized array for float weights[inp]; in class
or:
1>nn.cpp(58): error C2057: expected constant expression at line void setinputs (float in[inp]) {
or:
1>nn.cpp(62): error C2440: 'initializing' : cannot convert from 'int (__cdecl *)(unsigned short)' to 'unsigned int' for void setweights (float [inp]); in class
.
.
or:
std::vector<float> in1[inp]; for std::vector<float> in1[inp];
and errors like this
I put the complete upper part of my prog here (this works fine):
# include <iostream>
# include <fstream>
# include <ostream>
# include <cmath>
# include <ctime>
# include <iomanip>
float temp;
int iteration;
float vepsum;
float returnedepsum;
std::vector<float> w2(inps*hnumbers+hnumbers+onumbers*hnumbers+onumbers);
std::vector<float> min(inps+onumbers);// holds min of all inp+onumbers columns
std::vector<float> max(inps+onumbers); //holds max of all inp+onumbers columns
std::vector<float> desiredoutputs(onumbers);
srand((unsigned)time(NULL)); // random generator starter
for (i=0; i<patterns+vpatterns+testpatterns; i++) { //reads input data from aa.txt to original[][]
for (j=0; j<inps+onumbers; j++){
f1>> original[i][j];
//cout<< original[i][j];
if (original[i][j]>1) normalizationproess=true;
};
};
f1.close();
float temp;
int iteration;
float vepsum;
float returnedepsum;
std::vector<float> w2(inps*hnumbers+hnumbers+onumbers*hnumbers+onumbers);
std::vector<float> min(inps+onumbers);// holds min of all inp+onumbers columns
std::vector<float> max(inps+onumbers); //holds max of all inp+onumbers columns
std::vector<float> desiredoutputs(onumbers);
srand((unsigned)time(NULL)); // random generator starter
for (i=0; i<patterns+vpatterns+testpatterns; i++) { //reads input data from aa.txt to original[][]
for (j=0; j<inps+onumbers; j++){
f1>> original[i][j];
//cout<< original[i][j];
if (original[i][j]>1) normalizationproess=true;
};
};
f1.close();
Thank you it worked.
Those numbers are float because in text file we can assign them float numbers.
in the main function I have defined a function like:
void ff(float in1[inps]);
and I have changed it to:
void ff(std::vector<float> in1(inps));
but I get error:
1>dy-arr.cpp(82): error C2061: syntax error : identifier 'inps'
> Those numbers are float because in text file we can assign them float numbers.
What do you do if in the file 25.73 is given for inps (the number of inputs)?
Hint:
a. Truncate: std::size_t n = inps ;
b. round: std::size_t n = std::lround(inps) ;
> and I have changed it to:
> void ff(std::vector<float> in1(inps));
Thank you very much. almost done, but one thing remaining.
I have the function float* pso(void) {}; that is called by gbestweightsp=pso(); inside the main(), and the pointer float *gbestweightsp; is defined before main() too;
float* pso(void) should point to the array gbestweights which is defined as:
std::vector<float> gbestweights(hnumbers*inps+hnumbers+onumbers*hnumbers+onumbers); inside the float* pso(void) function.
I can see the contents of the gbestweights array inside the function, but when wanted to return it at the end of the function by:
gbestweightsp=gbestweights;
return gbestweightsp;
the gbestweightsp=gbestweights; gets error:
1>dy-arr.cpp(560): error C2440: '=' : cannot convert from 'std::vector<_Ty>' to 'float *'