Assign a list of values to a class
-
Hi: I have a matrix class and I would like to be able to fill in the elements by supplying a list of values. In other words, I would like to be able to write something like this: matrix A(2, 2) = {3, 4; 2, 1}; This would create a 2 x 2 matrix: 3 4 2 1 Is this possible? The only workaround that I can think of right now is to convert the right hand side to a string "3, 4; 2, 1" and then overload the assignment operator with something like this: matrix& operator = ( char *sString ); And then do a whole bunch of splits, trims, and searches within the string. Can anyone suggest a better solution? Thanks
-
Hi: I have a matrix class and I would like to be able to fill in the elements by supplying a list of values. In other words, I would like to be able to write something like this: matrix A(2, 2) = {3, 4; 2, 1}; This would create a 2 x 2 matrix: 3 4 2 1 Is this possible? The only workaround that I can think of right now is to convert the right hand side to a string "3, 4; 2, 1" and then overload the assignment operator with something like this: matrix& operator = ( char *sString ); And then do a whole bunch of splits, trims, and searches within the string. Can anyone suggest a better solution? Thanks
of course it's possible!
int a[2][2] = { {1,2} , {3,4} };
#include <beer.h>
-
of course it's possible!
int a[2][2] = { {1,2} , {3,4} };
#include <beer.h>
The thing is that I am assigning this array to a class, which has variables other than the 2-dimensional array or numbers (I don't think shallow assignment will do here): class matrix { private: double **_m; double *_v; int _nType, _nRows, _nCols; void _Copy( const matrix &m1 ); void _Destroy( ); public: // Default constructor matrix( int nMatrixType = mxMatrix, int nDim1 = 0, int nDim2 = 0 ); // Copy constructor matrix( const matrix &m1 ); ~matrix( ); matrix& operator = ( const matrix &m1 ); matrix operator * ( const matrix &m1 ); int GetType( ) { return _nType; } int GetRows( ) { return _nRows; } int GetCols( ) { return _nCols; } void Display( ) const; double GetElement( int nDim1, int nDim2 = 0 ) const; void SetElement( double fValue, int nDim1, int nDim2 = 0 ); matrix Transpose( ); matrix Solve( const matrix &mRHS ); };
-
Hi: I have a matrix class and I would like to be able to fill in the elements by supplying a list of values. In other words, I would like to be able to write something like this: matrix A(2, 2) = {3, 4; 2, 1}; This would create a 2 x 2 matrix: 3 4 2 1 Is this possible? The only workaround that I can think of right now is to convert the right hand side to a string "3, 4; 2, 1" and then overload the assignment operator with something like this: matrix& operator = ( char *sString ); And then do a whole bunch of splits, trims, and searches within the string. Can anyone suggest a better solution? Thanks
-
Does your design require dynamic allocation? In other words, does the program dynamically allocate matrices during run-time? If not, one solution is an STL container such as a list of paired values. Kuphryn
The program DOES allocate matrices dynamically; however, there is going to be a fixed 9 x 9 matrix with predetermined values that is going to be used in calculations (this matrix needs to be of the same type (class) as the other dynamically allocated matrices). I just want to avoid typing the following 81 times: m.SetElement(0, 0, 100.0); m.SetElement(0, 0, 200.0); ..... and replace it with something like m = { 100.0, 200.0, ... } Thanks.
-
The program DOES allocate matrices dynamically; however, there is going to be a fixed 9 x 9 matrix with predetermined values that is going to be used in calculations (this matrix needs to be of the same type (class) as the other dynamically allocated matrices). I just want to avoid typing the following 81 times: m.SetElement(0, 0, 100.0); m.SetElement(0, 0, 200.0); ..... and replace it with something like m = { 100.0, 200.0, ... } Thanks.
-
Hi: I have a matrix class and I would like to be able to fill in the elements by supplying a list of values. In other words, I would like to be able to write something like this: matrix A(2, 2) = {3, 4; 2, 1}; This would create a 2 x 2 matrix: 3 4 2 1 Is this possible? The only workaround that I can think of right now is to convert the right hand side to a string "3, 4; 2, 1" and then overload the assignment operator with something like this: matrix& operator = ( char *sString ); And then do a whole bunch of splits, trims, and searches within the string. Can anyone suggest a better solution? Thanks