values stored in public member variables

When the object a1 of class matrix is instantiated, the constructor matrix::matrix correctly gets the values for both public member variables namely no_of_rows_ and no_of_cols_ respectively.

However, the public method transpose() doesn't read those values. Instead it prints out some apparently garbage values (e.g. -947286536 and 32767) for those public member variables. Would anyone know why?

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
class matrix
{
   public:
      int no_of_rows_;
      int no_of_cols_;
      float ** matrix_;

      // Constructor
      matrix(int no_of_rows_, int no_of_cols_)
      {
         cout << no_of_rows_ << " " << no_of_cols_ << endl;
         matrix_ = new float * [no_of_rows_];
         for (int row=0; row < no_of_rows_; row++)
         {
              matrix_[row] = new float [no_of_cols_];
              for (int col=0; col < no_of_cols_; col++)
              {
                  cout << matrix_[row][col] << "\t";
              }
             cout << endl;
         }
      }

      float ** transpose()
      {
         cout << no_of_rows_ << " " << no_of_cols_ << endl;
         float ** matrix_t_ = new float * [no_of_cols_];
         for (int col=0; col < no_of_cols_; col++)
         {
            matrix_t_[col] = new float [no_of_rows_];
            cout << "Column:" << col << endl;
            for (int row=0; row < no_of_rows_; row++)
            {
               matrix_t_[row][col] = matrix_[col][row];
               cout << matrix_t_[row][col] << "\t";
            }
         }
         return matrix_t_;
      }
};

int main(int argc, char *argv[])
{
    matrix a1(atoi(argv[1]), atoi(argv[2]));
    float ** a1_t = a1.transpose();
}
You have used the parameters of the constructor though you have not assigned them to the member variables.
Topic archived. No new replies allowed.