Click here to Skip to main content
15,888,321 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
class matrix
    {
      public:

	// these are overloaded operators - 3 styles
	void operator = (const matrix);
	float * operator() (const int, const int) ;

	// this is a constructor function
	matrix (int m, int n)  // default initialization
	  {
	     rows = m;      //set the members
	     cols = n;
	     pt = new float[rows*cols];
	  }
	// these are the variable members - they are private
      private:
	int rows, cols;
	float *pt;
      };    // note the ;

  // overloaded definitions
  void matrix :: operator =(const matrix arg)
      {
	 int i;
	 if( (rows != arg.rows) || (cols != arg.cols) )
	   {
	     cout <<  "\n You cannot set different sized";
	     cout << " matrices equal to each other. \n";
	     //exit(EXIT_FAILURE);
	   }

	 for (i=0; i < rows*cols; i++)
	   *(pt+i) = *(arg.pt+i);
      }
  float * matrix :: operator() ( const int row, const int col)
      {
	 return pt + (row * cols + col);
      }

main (int argc, int *argv[])
 {
   int i, j;

   // We can run with variable matrix size without re-compiling
   if (argc != 3)    // the program name and 2 variables = 3
     {
       cout << "You must include 2 values with the name of the program\n"
	    << " matrix #rows #cols. \n "
	    << " Try again.\n";
      // exit (EXIT_FAILURE);
     }

   int rows = (argv[1]);   // I trust that the user
   int cols = (argv[2]);   // will input good values

   matrix A(rows, cols);
   matrix B(rows,cols);
   matrix C(rows-2,cols-2);

   for (i=0; i < rows; i++)
     for (j=0; j < cols; j++)
       *A(i,j) = i + j;

   B = A;

   cout << "\n The A matrix \n";
   for (i=0; i < rows; i++)
     {
       for (j=0; j < cols; j++)
	 cout << *A(i,j) << "  ";
       cout << "\n";
     }

   cout << "\n The B matrix \n";
   for (i=0; i < rows; i++)
     {
       for (j=0; j < cols; j++)
	 cout << *B(i,j) << "  ";
       cout << "\n";
     }

   C = A;

  }
Posted
Comments
Sergey Alexandrovich Kryukov 29-Nov-11 19:37pm    
Where can I see C++/CLI here? It all looks like pure C++...
--SA
Sergey Alexandrovich Kryukov 29-Nov-11 19:39pm    
And where can I see a char?!
--SA
Sergey Alexandrovich Kryukov 29-Nov-11 19:41pm    
OK, it's not nice to mention compiler error and warnings without posting of full error information and indicating each of them in the code sample comments. Do you like riddles? Use "Improve question".
--SA
enhzflep 29-Nov-11 19:54pm    
"main (int argc, int *argv[])"

Ha ha ha ha ha ha - I guess this wasn't an attempt at humour, so I'll tell you what's wrong with it (1) no return type - use int (2) _int_ *argv[] - don't think so mate!! change it to char *argv[]
argv is after all an array of pointers to each of the ARGument Values passed on the command line. These are strings, not ints!!

Secondly, the subject line is just that - You're not supposed to place your question there. Why not put your whole code snippet there too? :feeling-ill:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900