#include <iostream>
#include <vector>
usingnamespace std;
class indiv {
// data members
// functions
public:
int init_PM(indiv &PM, int m, int chr, int z); // declaration of init_PM as a member function of class indiv
//other functions
};
int indiv::init_PM(indiv &PM, int m, int chr, int z) // definition of init_PM as a member function of class indiv
{z=5;
return z;}
int main()
{
int init_PM(indiv PM, int m, int chr, int z); // init_PM declared in main()
int m=2; // set by 'cin>>' in full program
int chr=19; // set by 'cin>>' in full program
int z=0;
// other declarations
vector<indiv> PM(m); // create a vector of m* indiv data-types
z=PM.init_PM(PM, m, chr, z); // init_PM called; <<<<<<<< compiler error points to this line
cout<<"z now = "<<z<<endl; // to prove that z has changed to 5
// rest of main
}
I wondered at first if this was a problem with passing the vector as this is new to me, but removing the vector parameter makes no difference. (I have also checked that my "&"s match the example code given in https://www.geeksforgeeks.org/passing-vector-function-cpp/).
I tried adding "indiv::" before the declaration in main() and this just made matters worse (I got additional errors).
It's telling me that there is no member called init_PM in indiv, but,
as far as I can see, I have both declared and defined init_PM() as a member function of class indiv, and declared it at the beginning of main().
I am sure that I am making some incredibly basic (=stupid) mistake, but I cannot for the life of me see what it is. Please, can anyone see what I am doing wrong?
I tried removing the first parameter and got another error from within init_PM(). This may well be because the function is considerably more complicated than the version I put in the code above, calling other functions like:
PM.expand(chr);
the compiler told me that 'PM was not declared in this scope'. Although it was happy enough with other functions like PM.set_values(chr, z) and PM.print_out(chr) inside init_PM()!
I will have a look at constructors. This is really my first serious attempt to build a class and I am rather learning the process as I go along. So far I haven't really considered constructors beyond a default one to make sure I have something to put data into when I have created an instance of my class. Your suggestion is a good excuse for me to investigate them further.