Help with "this->"

My calculation will only return 1, and I think it's because I'm not using this-> correctly. I'm trying to turn a number from 1 to 10 into a number from 0.0 to 1.0.

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
#include <iostream>
#include <sstream>

using namespace std;

class test
{
public:
   double day;
   static const double MAX_VAL;
   static const double SCALE_FACTOR;

   double determineDay(test guy);
};

double const test::MAX_VAL = 10;
double const test::SCALE_FACTOR = 1;

int main()
{
   test man1, man2;
   
   man1.day = 3;
   man2.day = 7;
   
   cout << man1.determineDay(man2) << endl;
}

double test::determineDay(test guy1)
{
   double diff, fit;

   //right here
   diff = this->day - day;
   fit = MAX_VAL - SCALE_FACTOR - abs(diff);
   fit = fit / ((double)(MAX_VAL - SCALE_FACTOR));

   return fit;
}
Last edited on
The only place I see you using the this pointer is at line 33.

Both this->day and day refer to the same member variable, so diff will always be 0.

Did you mean:
diff = this->day - guy1.day;
Last edited on
That's probably closer to what I mean, yes, but in my actual program, I have 4 objects. What needs to happen is each object needs to compare their data to each other object. If I do it that way, would main need to call this method for each object?
Topic archived. No new replies allowed.