Hi! My assignment is essentially to get the constructor to return N^x, where N is the input and x is how many times the constructor is called.
Right now I'm just testing to make sure the initial case 3^0=1 works.
I keep getting errors in homework3_test.cpp saying "no match for call to '(PowerN) (int&)' "
Also, is it not allowed to return a value from a constructor?
This is my code so far:
#include "chai.h"
#include <vector>
#include <iostream>
#include <stdexcept>
#include <math.h>
using std::vector;
int main(int argc, char** argv){
//Testing the PowerN class
int test_power1, test_power2, test_power3;
PowerN power_three(3);
//test_power will now be 1
power_three(test_power1);
//test_power will now be 3
power_three(test_power2);
//test_power will now be 9
power_three(test_power3);
if (1 == test_power1) {
std::cout<<"PowerN works for 3**0! \n";
}
else {
std::cout<<"PowerN failed on 3**0!\n";
}
}
#include "chai.h"
#include <vector>
#include <iostream>
#include <stdexcept>
#include <math.h>
using std::vector;
int main(int argc, char** argv){
//Testing the PowerN class
int test_power1, test_power2, test_power3;
PowerN power_three(3);
//test_power will now be 1
power_three(test_power1);
//test_power will now be 3
power_three(test_power2);
//test_power will now be 9
power_three(test_power3);
if (1 == test_power1) {
std::cout<<"PowerN works for 3**0!\n";
}
else {
std::cout<<"PowerN failed on 3**0!\n";
}
}
int PowerN::operator()(int& x){
return pow(N,x);
x++;
}
x++ will never be executed in this function, since you return before it.
And AFAIK, if you want to increment the member x, and not the parameter, you must explicitly refer to it like : this->x++; because the parameter x will shadow the member x
My assignment is essentially to get the constructor to return N^x, where N is the input and x is how many times the constructor is called.
hmmm, do you mean to return from operator() instead ?