By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,824 Members | 2,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,824 IT Pros & Developers. It's quick & easy.

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

P: 74
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?

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h" 
  2. #include <iostream> 
  3. #include <cstdlib>
  4. #include <string> 
  5. using namespace std;   
  6.  
  7. class Matrix{ 
  8.     int m, n, i, j, first[10][10], second[10][10], sum[10][10]; 
  9.  
  10.   public:
  11.    void initMatrix(); 
  12.    void printMatrix(); 
  13.  
  14.  
  15. }; 
  16.  
  17. void Matrix::initMatrix() 
  18.      cout << "Enter the number of rows of matrices"; cin >> m;  
  19.      cout << "Enter the number of columns of matrices"; cin >> n; 
  20.      cout <<  "Enter the elements of first matrix\n"; 
  21.       for (i=0; i<m; ++i)
  22.       {
  23.         for (j=0; j<n; ++j)
  24.         {
  25.           cin >> first[i][j]; 
  26.         }
  27.       }
  28.       cout << "Enter the elements of second matrix\n";    
  29.  
  30.  
  31.       for (i=0; i<m; ++i)
  32.        {
  33.           for (j=0; j<n; ++j)
  34.           {
  35.             cin >> second[i][j];
  36.           }
  37.         }
  38.   } 
  39.  
  40.   void Matrix::printMatrix()
  41.   { 
  42.  
  43.         for(i=0; i< m; i++)
  44.        {
  45.           for(j=0; j<n; j++)
  46.           {
  47.              sum[i][j]= first[i][j] + second[i][j]; 
  48.            }
  49.         }
  50.  
  51.       // Print the sum matrix
  52.  
  53.        cout << "Sum of entered matrices\n";
  54.  
  55.        for(i= 0; i<m; i++)
  56.         { 
  57.           for(j=0; j<m; j++)
  58.           {
  59.              cout << sum[i][j] << "\t";
  60.           }
  61.         } 
  62.  } 
  63. int main() 
  64.           Matrix M1; 
  65.            cout << M1.initMatrix() << endl; 
  66.            cout <<  M1.printMatrix() << endl; 
  67.  
  68.  
  69.      cout << "Hit any key to continue" << endl; 
  70.  
  71.     system ("pause");   
  72.      return 0; 
  73. }
Apr 14 '17 #1
Share this Question
Share on Google+
2 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Your methods are returning void. cout can't deal with functions that do not return a value.

Instead of cout, all you need do is:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. Matrix M1;
  4. M1.initMatrix();
  5. M1.printMatrix();
  6. }
Apr 14 '17 #2

P: 74
@weaknessforcats,

Thank you for your help.
Apr 14 '17 #3

Post your reply

Sign in to post your reply or Sign up for a free account.