Why aren't my functions printing when I try to access them in the main function?

Hello Everyone,
I am working on a C++ application in which I create a Matrix Class which has my attributes and arrays and also my public methods, and then I create an instance of that class by creating an object to access two of my methods which is defined in the class declaration. How can I properly use two of those methods to output the information that is defined in those methods? Here is my code so far. What I am doing wrong?

#include "stdafx.h" 
#include <iostream> 
#include <cstdlib>
#include <string> 
using namespace std;   

class Matrix{ 
    int m, n, i, j, first[10][10], second[10][10], sum[10][10]; 
    
  public:
   void initMatrix(); 
   void printMatrix(); 
    

}; 

void Matrix::initMatrix() 
{ 
     cout << "Enter the number of rows of matrices"; cin >> m;  
     cout << "Enter the number of columns of matrices"; cin >> n; 
     cout <<  "Enter the elements of first matrix\n"; 
      for (i=0; i<m; ++i)
      {
        for (j=0; j<n; ++j)
        {
          cin >> first[i][j]; 
        }
      }
      cout << "Enter the elements of second matrix\n";    
 
     
      for (i=0; i<m; ++i)
       {
          for (j=0; j<n; ++j)
          {
            cin >> second[i][j];
          }
        }
  } 

  void Matrix::printMatrix()
  { 

        for(i=0; i< m; i++)
       {
          for(j=0; j<n; j++)
          {
             sum[i][j]= first[i][j] + second[i][j]; 
           }
        }
         
      // Print the sum matrix

       cout << "Sum of entered matrices\n";

       for(i= 0; i<m; i++)
        { 
          for(j=0; j<m; j++)
          {
             cout << sum[i][j] << "\t";
          }
        } 
 } 
int main() 
{ 
          Matrix M1; 
           cout << M1.initMatrix() << endl; 
           cout <<  M1.printMatrix() << endl; 
            

     cout << "Hit any key to continue" << endl; 
 
    system ("pause");   
     return 0; 
} 

Hi,

I noticed that in your two methods, initMatrix() and printMatrix() you include the cout<< function to print to the screen, then in main() when you call the object M1 and the two methods you again try to use cout<< causing an error.

Try calling M1.initMatrix(); and M1.printMaxtrix(); without using cout<< under your main function.

Steve

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.