Method return type?

Jun 3, 2013 at 4:30am
What's wrong with my code below? Throws this exception:

matrix.cpp:49:24: error: argument of type ‘int (matrix::)()’ does not match ‘float**’

Here's the source code:

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

      // Constructor
      matrix(int no_of_rows_, int no_of_cols_)
      {
         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()
      {
         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_];
            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;
}
Jun 3, 2013 at 6:00am
Line 42: add parentheses to the function call.
Jun 4, 2013 at 12:47am
Here's something interesting. 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 please?

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();
}

Jun 4, 2013 at 5:10am
You don't set the member variables in the ctor. Do not use member-name as parameter-name.
Jun 4, 2013 at 7:25am
Thanks! I have a new problem now. Why is the transposed matrix printing out garbage values in column 7?

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

      // Constructor
      matrix(int n_rows_, int n_cols_)
      {
         no_of_rows_ = n_rows_;
         no_of_cols_ = n_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 << "[" << row << "," << col << "]" << 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_];
            for (int row=0; row < no_of_rows_; row++)
            {
               cout << "[" << col << "," << row << "]" << matrix_[col][row] << "\t";
               //matrix_t_[row][col] = matrix_[col][row];
            }
            cout << endl;
         }
         return matrix_t_;
      }
};

int main(int argc, char *argv[])
{
    matrix a1(atoi(argv[1]), atoi(argv[2]));
    float ** a1_t = a1.transpose();
}


Here is the output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
10 5
[0,0]0	[0,1]0	[0,2]0	[0,3]0	[0,4]0	
[1,0]0	[1,1]0	[1,2]0	[1,3]0	[1,4]0	
[2,0]0	[2,1]0	[2,2]0	[2,3]0	[2,4]0	
[3,0]0	[3,1]0	[3,2]0	[3,3]0	[3,4]0	
[4,0]0	[4,1]0	[4,2]0	[4,3]0	[4,4]0	
[5,0]0	[5,1]0	[5,2]0	[5,3]0	[5,4]0	
[6,0]0	[6,1]0	[6,2]0	[6,3]0	[6,4]0	
[7,0]0	[7,1]0	[7,2]0	[7,3]0	[7,4]0	
[8,0]0	[8,1]0	[8,2]0	[8,3]0	[8,4]0	
[9,0]0	[9,1]0	[9,2]0	[9,3]0	[9,4]0	
10 5
[0,0]0	[0,1]0	[0,2]0	[0,3]0	[0,4]0	[0,5]0	[0,6]4.62428e-44	[0,7]0	[0,8]0	[0,9]0	
[1,0]0	[1,1]0	[1,2]0	[1,3]0	[1,4]0	[1,5]0	[1,6]4.62428e-44	[1,7]0	[1,8]0	[1,9]0	
[2,0]0	[2,1]0	[2,2]0	[2,3]0	[2,4]0	[2,5]0	[2,6]4.62428e-44	[2,7]0	[2,8]0	[2,9]0	
[3,0]0	[3,1]0	[3,2]0	[3,3]0	[3,4]0	[3,5]0	[3,6]4.62428e-44	[3,7]0	[3,8]0	[3,9]0	
[4,0]0	[4,1]0	[4,2]0	[4,3]0	[4,4]0	[4,5]0	[4,6]4.62428e-44	[4,7]0	[4,8]0	[4,9]0	



Jun 4, 2013 at 8:18am
You have to think more carefully, which index is row and col for the original and the transpose.
Jun 5, 2013 at 1:26am
Thank you! I managed to fix it now. Below output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
8 4
[0,0]0	[0,1]1	[0,2]2	[0,3]3	
[1,0]4	[1,1]5	[1,2]6	[1,3]7	
[2,0]8	[2,1]9	[2,2]10	[2,3]11	
[3,0]12	[3,1]13	[3,2]14	[3,3]15	
[4,0]16	[4,1]17	[4,2]18	[4,3]19	
[5,0]20	[5,1]21	[5,2]22	[5,3]23	
[6,0]24	[6,1]25	[6,2]26	[6,3]27	
[7,0]28	[7,1]29	[7,2]30	[7,3]31	
8 4
[0,0]0	[0,1]4	[0,2]8	[0,3]12	[0,4]16	[0,5]20	[0,6]24	[0,7]28	
[1,0]1	[1,1]5	[1,2]9	[1,3]13	[1,4]17	[1,5]21	[1,6]25	[1,7]29	
[2,0]2	[2,1]6	[2,2]10	[2,3]14	[2,4]18	[2,5]22	[2,6]26	[2,7]30	
[3,0]3	[3,1]7	[3,2]11	[3,3]15	[3,4]19	[3,5]23	[3,6]27	[3,7]31	

Topic archived. No new replies allowed.