I am new to C++ and am attempting to apply some simple codes that run well in Matlab to C++. In the case here, I create a vector of n (here 1000) uniform random doubles representing fish lengths ("Cohort"). The cohort is then partitioned into individual that mature at the start ("year 1, spawners1) and the remainder ("Immature1") that remains immature until the following year (if they survive). The Immature1s are then partitioned into survivors that spawn in year 2 ("Spawners2") and the remainder that did not survive ("Morts2").
I have attempted to use vectors for these quantities because the number of Immature1swill be less than n (1000) and so the second "for ..." loop will have to ask for iterations from 1 to the length of Immature1.
I have included the error messages that arise in Visual Studio when I try to build.
I simply do not understand why the two types of erros arise.
I would much appreciate how best to code this simple problem, and to understand why the errors arise.
int main()
{
ofstream fout1("C:\\Cpp\\VSProjects\\June4\\Cohort.txt");//initial cohort at first age-at-maturity
ofstream fout2("C:\\Cpp\\VSProjects\\June4\\R1.txt");//unif randoms for probability of maturation
ofstream fout3("C:\\Cpp\\VSProjects\\June4\\R2.txt");// unif randoms for probability of survival of initial cohort immatures
ofstream fout4("C:\\Cpp\\VSProjects\\June4\\Mat1.txt"); //Age1 Matures
ofstream fout5("C:\\Cpp\\VSProjects\\June4\\Immat1.txt");//Age1 Immatures
ofstream fout6("C:\\Cpp\\VSProjects\\June4\\Morts1.txt");//Age 1 immatures that do not survive to age 2.
ofstream fout7("C:Cpp\\VSProjects\\June4\\Age2.txt");//Age2 survivors, both maturing and immatures
Spawners1[i] is not a vector ... it is an element of a vector.
For Spawners1[i].push_back(Cohort[i])
you presumably meant Spawners1.push_back(Cohort[i])
Similarly for the other mentioned errors.
It won't work after that because Cohort[i] doesn't exist. You declared Cohort as an empty vector in the line std::vector<double> Cohort;
and you would have to either declare it with the desired size (probably nreps), or use push_back() to gradually increase it, or use resize() to fix its desired size.
Please:
(1) USE CODE TAGS
(2) Don't put so much code down before you start trying to compile it.
Of course, the size of each vector is not always nreps, hence the blanks.
You will have to work out how you handle that aspect. Perhaps push_back a value instead of placing it at a particular indexed location in the <vector>, even use separate indices.
Perhaps <vectors> aren't such a good idea after all, maybe not even needed at all.