Help with scope and operator overloading

May 7, 2014 at 2:22pm
Hi! I'm having trouble getting my PowerN function to behave in the intended manner. I want to have a PowerN so that the constructor, with an inputted integer (N), would set the N value. Then, every time the PowerN object is called with a variable in the parameter, the value of the inputted variable will become N^x.

For instance:
int x,y
PowerN powerObject(3); // N is now 3
powerObject(x); // x is now 3^0 = 1
powerObject(x); // x is now 3^1 = 3
powerObject(y); // y is now 3^2 = 9


My current code runs, but fails the tests. I can't figure out how to increment the i value in order to change the input variables.

This is my code so far:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;


class PowerN{

	public:
		PowerN();	// Default constructor
		PowerN(int);
		void operator()(int& x);

	private:
		int N;
		static int i;

};

PowerN::PowerN(){}	// Called when the constructor is called alone

PowerN::PowerN(int i){	// Called when the constructor is called with an integer variable

	int N = i;	// Takes in an integer and sets N to that value

}

int PowerN::i = 0;

void PowerN::operator()(int& x){	// Overloading the () operator since we're calling x several times.
	i++;	// Increments i every time an object of PowerN () is called
	x = pow(N,i);

}


int main(){

	//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) {
		cout<<"PowerN works for 3^0!\n";
	}
	else {
		cout<<"PowerN failed on 3^0!\n";
	}

	if (3 == test_power2 && 9 == test_power3) {
		cout<<"PowerN works for 3^1 and 3^2!\n";
	}
	else {
		cout<<"PowerN failed for 3^1 and 3^2!\n";
	}

	return 0;
}
Last edited on May 7, 2014 at 5:55pm
May 7, 2014 at 5:55pm
Any input would be much appreciated :D
May 7, 2014 at 6:23pm
See comment on line 3:
1
2
3
4
5
void PowerN::operator()(int& x){	
	i++;	
	x = pow(N,i); // i == 1 here the first call; 2 the second call ...

}
May 7, 2014 at 7:15pm
Ah, whoops. I changed i++ so it came after line 3 and forgot to update. But when I did so, the first test would pass, but the second test would fail (the "3 == test_power2 && 9 == test_power3" test).
May 7, 2014 at 8:27pm
Read the following: http://en.wikipedia.org/wiki/Variable_shadowing

then look at line #25
May 8, 2014 at 3:07pm
Thank you! That was uber helpful. My biggest weakness is scope issues.


Hm, I changed my variable name from i to n in the PowerN(int) constructor, but my second test is still failing D:
May 8, 2014 at 3:26pm
All you needed to to do was remove "int" from line 25. N = i;

With "int" you are declaring another variable named "N" which is local to the constructor and shadows masks the class member N. The local variable goes out of scope at the end of the ctor block and the class member "N" remains uninitialized.

Look into constructor initialization lists: http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/
PowerN::PowerN(int i) : N(i) { }

HTH

edit: changed shadows to masks
Last edited on May 8, 2014 at 5:59pm
Topic archived. No new replies allowed.