Problem with variable initialization within a class

Before I start I would just like to say that I am very new to C++ and programming in general, so my structure isn't the greatest when it comes to making programs. Now that that's out of the way, let me introduce you to my problem. Within a constructor in a class I have made called Matt I am trying to set the values of the variables within the class. I am successful in doing this but then I call one of my non-member functions within the constructor and some unexpected results are produced. The variables after the function is over and the constructor is finished are extremely large (indicating that the variables were not initialised correctly, which I don't understand). Here is my constructor:

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
Jeff::Jeff(const char rawData[ ], int freqJ, int aJ, int bJ) {
   signal[0] = '\0';
   
   if(freqJ > 9 || freqJ  <= 0) {
      freqC = 0;
   }
   else { 
      freqC = freqJ;
   }
   if(aJ >= 0 && aJ <= 255) {
      aC = aJ;
   }
   else {
      aC = 0;
   }
      if(bJ >= 0 && bJ <= 255) {
      bC = bJ;
   }
   else {
      bC = 0;
   }
   
   printf("a: %d, b: %d, frequency: %d\n", aC, bC, freqC);
   analyze(rawData, signal, freqC, aC, bC);
   printf("a: %d, b: %d, frequency: %d\n", aC, bC, freqC);
}


In the first printf, the values I expect to see are displayed:

OUTPUT: a: 32, b: 122, frequency: 3

But after the function is finished, (and you are going to have to trust me that I am not changing the values of a b or freq within the analyze function, I am just using them for if statements and loops etc. I know this because I tested the function's code separately and it did not change the variables used at all. But even still, in a non member function the variables used are just copies so it would not change the member variables if I recall correctly) this is displayed:

OUTPUT: a: 1785493355, b: 2003396199, frequency: 1079912744

signal is a member char string that gets filled from scanning through the rawData block in analyze function and uses the freq, a, and b variables as conditions for filling, but I do not believe this would affect the output.

I know this is probably a problem with initialisation but it does not make sense to me since the values are properly initialised from what I see. If there is anything glaringly obvious please feel free to lead me on the right path. Thank you in advance
Last edited on
i'm not sure but the problem seems to be where you are trying to change the signal array. passing arrays as arguments and then changing their values in the function are possible sources of errors.

you can read the following article to check if you are doing the correct thing.

http://www.cplusplus.com/doc/tutorial/pointers/

also please post the following parts of your code to help us understand further:
1. analyze function
2. the entire class definition (how you are declaring member variables etc).
Topic archived. No new replies allowed.