Reputation: 215
When I try compiling the code below, I got the error following:
homework3_test.cpp:111:25: error: no match for call to ‘(PowerN) (int&)’ power_three(test_power2);
PowerN is a class.
class PowerN{
public:
static int i;
PowerN(int a);
};
power_three is defined as
PowerN power_three(3);
this one is fine while "3" is an integer. But for the following three integer variable:
int test_power1, test_power2, test_power3;
it returns the error. So why are they different? Is it the reason that int test_power1 does not have a value so it becomes a int& ? How may I solve that?
below is the code of PowerN
PowerN::PowerN(int a){
int b=0;
if (i>0){
a = pow(b,(i-1));
}
else {
b=a;
}
i= i+1;
}
Update: PowerN need to change the value of the 2nd, 3rd,4th integer it takes, below is the requirement:
PowerN's constructor takes a single integer, N. Every time the function operator of an instance of PowerN is called, it changes the value of its argument to N**x, where x is the number of times the function operator was called. For example:
int x;
PowerN power_three(3);
power_three(x);//x will now be 1
power_three(x);//x will now be 3
power_three(x);//x will now be 9
The code that reproduces error is:
homework3_test.cpp:111:25: error: no match for call to ‘(PowerN) (int&)’
power_three(test_power2);
Upvotes: 0
Views: 411
Reputation: 141544
The problem appears in the code in your update:
int x;
PowerN power_three(3);
power_three(x);//x will now be 1
The last line attempts to call PowerN::operator()
. However you did not define an operator()
. The error message is a bit cryptic, but it is saying that it looked for PowerN::operator()(int &)
and did not find it.
I don't know what you are imagining will happen that would result in "x will now be 1". It almost seems as if you are trying to call the constructor again on an object that already exists. That is not legal; and even if it were, the syntax would be something different.
Perhaps you should move some of the code currently in your constructor into a member function (say calculate(int &)
, and then do:
PowerN power_three(3);
power_three.calculate(x);
Upvotes: 2