'class std::vector<indiv>' has no member named 'init_PM'

I am getting the above compiler error when I try to run the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
using namespace 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?

Thanks.
Last edited on
PM is a vector<indiv> which doesn't have a init_PM function.

Did you perhaps mean to call init_PM on one of the m indiv objects in PM (which one?).

1
2
// calls init_PM on the first vector element and passes the second vector element as argument.
z = PM[0].init_PM(PM[1], m, chr, z);
Thanks for the reply, Peter87.

I knew it was going to be something stupid.

In the full program, init_PM() sets the intial values for each member of the vector, so I am going to have to call it for each of the m members.

I have replaced lines 26 & 27 with:

1
2
3
4
for(x=0; x<m; x++){
	z=PM[x].init_PM(PM[x], m, chr, z);
	cout<<"returned from init_PM("<<x<<"): "<<z<<endl;


and tested it. It appears to be performing as required.

Thanks.
There is not really much point in the first parameter if you are always going to pass the same object that the function is called on.

1
2
3
z=PM[x].init_PM(PM[x], m, chr, z);
    ^             ^
  These two always the same?


In C++ we normally use constructors to initialize class objects. It's another alternative that you might want to consider.
Last edited on
Thanks for the suggestions.

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.

Thanks.
Registered users can post here. Sign in or register to post.