Jan 14, 2011 at 9:36am UTC
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 Jan 14, 2011 at 9:42am UTC
Jan 14, 2011 at 11:21am UTC
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 != ¶m) {
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 Jan 14, 2011 at 11:21am UTC
Jan 14, 2011 at 11:43am UTC
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 Jan 14, 2011 at 11:44am UTC
Jan 14, 2011 at 12:16pm UTC
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 Jan 14, 2011 at 12:18pm UTC
Jan 15, 2011 at 1:35am UTC
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
Jan 18, 2011 at 12:53am UTC
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 != ¶m) {
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 Jan 18, 2011 at 1:00am UTC
Jan 18, 2011 at 12:16pm UTC
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.
Jan 18, 2011 at 12:29pm UTC
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 Jan 18, 2011 at 12:41pm UTC