I have two different versions of the constructor for my “Policy” class. I can’t seem to pass a Policy object by reference when using one of the constructors, and I don’t understand why not.
Overloaded constructors:
Policy::Policy(int testing) {
initAge=160;
initState=1;
reset();
x[0]=4;
x[1]=2;
ub[0]=10;
ub[1]=10;
lb[0]=0;
lb[1]=0;
}
Policy::Policy() {
initAge=160;
initState=1;
reset();
x[FRQ]=4;
x[BEG]=40*4;
x[END]=75*4;
for(int i=0; i<240; i++)
x[2+i]=4.0;
x[GS]=2;
lb[0]=1;
ub[0]=80;
for(int i=1;i<3;i++) {
lb[i]=160;
ub[i]=400;
}
for(int i=3;i<243;i++) {
lb[i]=1;
ub[i]=16;
}
lb[243]=2;
ub[243]=4;
}
Here’s my code that gives the error.
void sampleMIXD(Constraints& space, Policy& p);
int main(int argc, char** argv) {
// Policy policy(1); // Works
Policy policy(); // Does not work
Constraints space(2);
sampleMIXD(space, policy);
return 0;
}
Error message:
'../main.cpp:64: error: invalid initialization of non-const reference of type 'Policy' from a temporary of type 'Policy (*)()' ../MIXD.h:12: error: in passing argument 2 of 'void sampleMIXD(Constraints&, Policy&)'
If instead of Policy policy(); I compile with Policy policy(1);, then the compiler doesn’t complain. Please help! 🙂
That’s the most vexing parse. Try
Basically, the compiler treats your variant as a declaration of a function taking nothing and returning policy. It’s similar to