define a friend function to built diagonal matrix

Hello friends.
I am developing a matrix class and there I encountered a problem when I tried to build a friend function to build a diagonal matrix. compiler confuse my function with the constructor of the class and give me the matrix is not declared in the class. where is my mistake?
name of the function is " diag ";
thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  
#include<iostream>
#include "matrix.h"

using namespace std;

int main()
{
    unsigned M=4;
    unsigned N=4;
    //unsigned *siz=new unsigned [2];


    matrix p1(M,N,0.0),p2(M,N,0.0),p3(M,N,0.0),p4(1,N,0.0),p7(M,1,0.0);
    matrix siz(1,2,0.0);



    
    matrix p12 = diag(M,0,3.0);
    p12.print();

}

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "matrix.h"
using namespace std;


matrix::matrix(unsigned rowSize,unsigned colSize, double initial):rowSize(rowSize),colSize(colSize)
{
    unsigned M_size = rowSize;
    unsigned N_size = colSize;
    my_matrix.resize(M_size);
    for(unsigned i=0;i<M_size;i++)
    {
        my_matrix[i].resize(N_size,initial);
    }

}
void matrix::print()
{
    for(unsigned i=0;i<rowSize;i++)
    {
        for(unsigned j=0;j<colSize;j++)
        {
          cout<<setw(3)<<my_matrix[i][j];
        }
        cout<<endl;
    }
}
double &matrix::operator()(const unsigned &rowNo,const unsigned &colNo)
{
    return this->my_matrix[rowNo][colNo];
}

matrix matrix::operator+(matrix &B)
{
    matrix sum(rowSize,colSize,0.0);
    unsigned i,j;
    for(i=0;i<rowSize;i++)
    {
        for(j=0;j<colSize;j++)
        {
            sum(i,j)=this->my_matrix[i][j] + B(i,j);
        }
    }
    return sum;
}
matrix matrix::operator-(matrix &B)
{
    matrix sub(rowSize,colSize,0.0);
    unsigned i,j;
    for(i=0;i<rowSize;i++)
    {
        for(j=0;j<colSize;j++)
        {
            sub(i,j)=this->my_matrix[i][j] - B(i,j);
        }
    }
    return sub;
}
matrix matrix::operator*(double val)
{
    matrix mult_scalar(rowSize,colSize,0.0);
    unsigned i,j;
    for(i=0;i<rowSize;i++)
    {
        for(j=0;j<colSize;j++)
        {
            mult_scalar(i,j)=this->my_matrix[i][j]*val;
        }
    }
    return mult_scalar;
}

matrix matrix::get_row(const unsigned &rowNo)
{

    matrix row_v(1,colSize,0.0);
    unsigned j;
    for(j=0;j<colSize;j++)
    {
        row_v(0,j)=this->my_matrix[rowNo][j];

    }
    return row_v;
}
matrix matrix::get_col(const unsigned &colNo)
{

    matrix col_v(rowSize,1,0.0);
    unsigned i;
    for(i=0;i<rowSize;i++)
    {
        col_v(i,0)=this->my_matrix[i][colNo];

    }
    return col_v;
}
/*unsigned* matrix::mat_size()
{
    unsigned *size_mat = new unsigned [2];
    size_mat[0]=rowSize;
    size_mat[1]=colSize;
    return size_mat;
}*/
matrix matrix::mat_size() const
{
    matrix size_mat(1,2,0.0);
    size_mat(0,0)= (double)rowSize;
    size_mat(0,1)= (double)colSize;
    return size_mat;
}
unsigned matrix::row_size() const
{
    return this->rowSize;
}
unsigned matrix::col_size() const
{
    return this->colSize;
}

matrix matrix::operator*(matrix &B)
{
      matrix mult(rowSize,colSize,0.0);
      unsigned i,j,k;
      for(i=0;i<rowSize;i++)
      {
          for(j=0;j<colSize;j++)
          {
              double sum = 0.0;
              for(k=0;k<colSize;k++)
              {
                  sum = sum + this->my_matrix[i][k]*B(k,j);
              }
              mult(i,j) = sum;
          }
      }

      return mult;
}

matrix diag(unsigned &dimension,int distanc,double value)
{
    matrix diag_mat(dimension,dimension,0.0);
    unsigned i;
    for(i=0;i<dimension;i++)
    {
        if(distanc >= 0)
        {
          diag_mat(i,i+distanc) = value;
        } else
        {
           diag_mat(i+distanc,i)=value;
        }
    }
    return diag_mat;
}

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
#ifndef matrix_class
#define matrix_class

#include<iostream>
#include<iomanip>
#include<vector>



using namespace std;
class matrix
{
private:
    unsigned rowSize,colSize;
    vector<vector<double> > my_matrix;
public:
    matrix(unsigned ,unsigned, double);
    void print();
    double &operator()(const unsigned &,const unsigned &);
    matrix operator+(matrix &);
    matrix operator-(matrix &);
    matrix get_row(const unsigned &);
    matrix get_col(const unsigned &);
    unsigned row_size() const;
    unsigned col_size() const;
    //unsigned* mat_size();
    matrix mat_size() const;
    matrix operator*(matrix &);
    matrix operator*(double );
    friend matrix Thridiag(matrix &B,matrix &C);
    friend matrix diag(unsigned &,int distanc,double value);//this function buld diagonal matrix with distance from diagonal with value

};
#endif
Last edited on
diag has 3 arguments. You call it (in main) with only 1.

That function will fail anyway if distance is non-zero, because you will try to access elements outside bounds.
if i eliminate the( for loop ) the error yet keep on in code
You can move the prototype for diag() OUTSIDE of class matrix in file matrix.h. It has no need to be a friend function anyway, as it does not require access to private members.

There is still no point to the argument distance, as the function would fail if it was anything other than zero.
Topic archived. No new replies allowed.