Constructors?

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:

chai_test.cpp:
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
#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";
	}
}



chai.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef CHAI_H
#define CHAI_H

#include <vector>
#include <iostream>

using namespace std;


class PowerN{

	public:
		PowerN();
		PowerN(int N);

};


#endif 



chai.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "chai.h"
#include <vector>
#include <iostream>
#include <stdexcept>
#include <math.h>

using std::vector;


PowerN::PowerN(){}

PowerN::PowerN(int N){

	int x = 0;
	std::cout pow(N,x);
	x++;

}
Last edited on
have you linked the files
By linking the files, do you mean including both .cpp files when compiling? If so, then yes.
I keep getting errors in homework3_test.cpp saying "no match for call to '(PowerN) (int&)' "


1
2
3
4
5
int test_power1, test_power2, test_power3;
PowerN power_three (3);

//test_power will now be 1
power_three (test_power1); // <== 


What you're trying to do is call PowerN::operator() ( int )
And i think that's not what you want.

Also, is it not allowed to return a value from a constructor?

Yes, constructors cannot return anything, even putting void is illegal
Last edited on
I'm trying to use PowerN::operator() (int), but I'm having trouble figuring it out.

I modified my code to this:

chai_test.cpp:
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
#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";
	}
}



chai.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef CHAI_H
#define CHAI_H

#include <vector>
#include <iostream>

using namespace std;


class PowerN{

	public:
		PowerN();
		PowerN(int);

		int x = 0;
		int N;
		int operator()(int&);


};


#endif 



chai.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "chai.h"
#include <vector>
#include <iostream>
#include <stdexcept>
#include <math.h>

using std::vector;


PowerN::PowerN(){}

PowerN::PowerN(int i){

	int N = i;

}

int PowerN::operator()(int& x){
	return pow(N,x);
	x++;
}
Last edited on
1
2
3
4
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 ?
Last edited on
Oh, I see. Whoops


Yes, I meant returning from operator()
Topic archived. No new replies allowed.