operator of template class

Is there a way to set it up so that the complex class when set = to a double its makes (double,0)

heres what I'm tryin:

1
2
3
4
complex::operator=(double param)
{
   return complex (double,0);
}


and on that same note I have a class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template <class T>
class Matrix {
    unsigned short rows, columns;
    T ** array;
    public:
    Matrix<T>(unsigned short newrows, unsigned short newcolums){
        rows=newrows;
        columns=newcolums;
        T ** array = Matrices::GenerateMatrix<T>(rows,columns);
    }
    Matrix<T>::operator=(const Matrix param){
        array=param.array;
        rows=param.rows;
        columns=param.columns;
        return * this;
    }
    Matrix operator + (Matrix);
    Matrix operator - (Matrix);
    Matrix operator * (Matrix);
    Matrix operator / (Matrix);
    ~Matrix();
};


now I can not seem to get the operator equal to stop erroring out. What do I need to do in a template class to get the = operator to work.
Last edited on
This:
1
2
3
4
5
6
7
8
9
10
template <class T>
class Matrix {
    //...
    Matrix<T>::operator=(const Matrix param){
        array=param.array;
        rows=param.rows;
        columns=param.columns;
        return * this;
    }
};

shoudl be:
1
2
3
4
5
6
7
8
9
10
11
12
template <class T>
class Matrix {
    //...
    Matrix<T> operator=(const Matrix<T>& param){
        if (this != &param) {
            array=param.array;
            rows=param.rows;
            columns=param.columns;
        }
        return * this;
    }
};


And on your complex thing, std::complex already has defaulted zero parameters.
Last edited on
And on your complex thing, std::complex already has defaulted zero parameters.


what I meant was:

1
2
3
4
//operator code here
std::complex mycomplex = 12;
//now this is equivalent to:
std::complex mycomplex (12,0);


and thank you
Last edited on
You can already write:
 
std::complex<double> n = 12.0;


I should point out that although the syntax on the assignment code I geve you is correct, it's probably doing the wrong thing.
Last edited on
what I really wanted was I have functions to generate matrices and when I generate them I set all the values to =0.

How can I make it to where complex and doubles can both use this template
ok so heres my 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Matrix {
    unsigned short rows, columns;
    T ** array;
    public:
    Matrix<T>(unsigned short newrows, unsigned short newcolums){
        rows=newrows;
        columns=newcolums;
        array = Matrices::generatematrix<T>(rows,columns);
    }
    Matrix<T> operator=(const Matrix<T>& param){
        if (this != &param) {
            array=param.array;
            rows=param.rows;
            columns=param.columns;
        }
        return * this;
    }
    Matrix<T>operator*(double param)
    {
        Matrix<T> temp(rows,columns);
        for (int i=0;i<rows;i++)
        {
            for (int j=0;j<columns;j++)
            {
                temp.array[i][j]=array[i][j]*param;
            }
        }
        return temp;
    }
    void stdprint()
    {
        for (int i=0;i<rows;i++)
        {
            for (int j=0;j<columns;j++)
            {
                std::cout<<array[i][j]<<" ";
            }
            std::cout<<"\n";
        }
    }
    void setvalue(T value,unsigned short row,unsigned short column)
    {
        array[row][column]=value;
    }
}

int main()
{
    while(1)
    {
        Matrix<double> bob(2,2);
        double jared = 3;
        for (int i=0;i<2;i++)
        {
            for (int j=0;j<2;j++)
            {
                bob.setvalue(i+j,i,j);
            }
        }
        bob.stdprint();
        bob=bob*jared;
        bob.stdprint();
        system("PAUSE");
    }
}


My output is:
0 1
1 2

then:
something*e-307 something*e-311
3 6

obviously then should be:
0 3
3 6

Now when I have this print when its in the operator= assignment it properly outputs:
0 3
3 6

so what is wrong within this piece?
Last edited on
Line 18 should be:
 
Matrix<T> operator*(double param)
It's possible you got away with it though.

Are you familiar with the Canonical Form of a class? A C++ class has four members generated automatically. A default constructor, copy constructor, assignment operator and destructor.

The defaults versions of these functions do the right thing if your class only has other object values. But if it contains pointers that managed dynamic objects they don't, and you need to provide your own.

You have only provided a copy operator. But code like:
1
2
3
4
5
Matrix<T> operator*(double param)
    Matrix<T> temp(rows,columns);
    //...
    return temp;
}
calls the copy constructor and the destructor. So you need to provide those.

The copy constructor looks like:
1
2
3
4
5
6
Matrix::Matrix<T>(const Matrix<T> &n)
    :   rows(n.rows),
        columns(n.columns),
        array(Matrices::generatematrix<T>(rows, columns))
{
}
Ha, okay read up on that but I am still confused on the copy constructor. How can I properly pass the values from n to the new object if the values are being pointed at. I.e.
array(Matrices::generatematrix<T>(rows, columns))
allocates the proper memory for the array but does nothing to pass the values contained.
I should point out that although the syntax on the assignment code I geve you is correct, it's probably doing the wrong thing.

The functions I gave you don't actually copy the values. Full versions are:
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
Matrix::Matrix<T>(const Matrix<T> &n)
    :   rows(n.rows),
        columns(n.columns),
        array(Matrices::generatematrix<T>(rows, columns))
{
    *this = n;
}

Matrix<T> operator=(const Matrix<T>& n)
{
    if (this != &n)
    {
        if (rows != n.rows || cols.!= n.cols)
        {
            // delete array--I don't know what code you have that'll do that
            rows = n.rows;
            cols = n.cols;
            array = Matrices::generatematrix<T>(rows, columns);
        }

        for (unsigned_short row = 0; row != rows; ++row)
            for (unsigned_short col = 0; col != cols; ++col)
                array[row][col] = n.array[row][col];
    }
    return *this;
}


The copy constructor and copy operators have to copy the data, right?

The data is held in the dynamically created array. So that data structure must be copied.
Last edited on
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
Matrix<T> operator=(const Matrix<T>& n)
{
    if (this != &n)
    {
        if (rows != n.rows || columns!= n.columns)
        {
            delete[]array;
            rows = n.rows;
            columns = n.columns;
            array = Matrices::generatematrix<T>(rows, columns);
        }
        for (unsigned short row = 0; row<rows; row++)
        {
            for (unsigned short col = 0; col<columns; col++)
            {
                array[row][col] = n.array[row][col];
            }
        }
    }
    return *this;
}
    Matrix<T>(const Matrix<T> &n)
    :   rows(n.rows),
        columns(n.columns),
        array(Matrices::generatematrix<T>(rows, columns))
    {
        for (unsigned short row = 0; row<rows; row++)
        {
            for (unsigned short col = 0; col<columns; col++)
            {
                array[row][col] = n.array[row][col];
            }
        }
    }


tired the code you gave me but the copy constructor would loop til crash. Used the code above and it worked. Is there anything wrong with the code above?
http://www.cplusplus.com/forum/beginner/33143/#msg179234
You should return a reference from operator=
Quite right, my mistake.
Topic archived. No new replies allowed.