Overloading the << for a matrix

Hello,

I am trying to overload the << operator a matrix class and am having some difficulty.

I read in data for the two matrices and read the data back:

int rows1, columns1, rows2, columns2;
cout << "Enter the number of rows and columns for the first matrix: " << endl;
cin >> rows1 >> columns1;
cout << "You entered " << rows1 << " rows" << " and " << columns1 << " columns for the first matrix." << endl;

cout << "\nEnter the number of rows and columns for the second matrix: " << endl;
cin >> rows2 >> columns2;
cout << "You entered " << rows2 << " rows" << " and " << columns2 << " columns for the second matrix." << endl;

I then try to output the matrices:

cout << matrix1;
cout << matrix2;

I was not getting any output, so I created a buffer. The buffer reads:

c:\users\ragan e. lake\documents\visual studio 2015\projects\assignment2rl_test3\assignment2rl_test3\main.cpp(132): warning C4715: 'operator<<': not all control paths return a value

Research online said that there is not a value for the control path, so I added an if else statement to see if my variables for number of rows and number of columns were not being populated.

Here is my code for the function:

ostream& operator<<(ostream& osObject, const matrixType& matrixA)
{
if (matrixA.numberOfRows || matrixA.numberOfColumns != NULL)
{
for (int i = 0; i < matrixA.numberOfRows; ++i)
{
for (int j = 0; j < matrixA.numberOfColumns; ++j)
{
// osObject << " " << matrix1.data[i * matrix1.numberOfColumns + j];
osObject << " " << (matrixA.board[i][j]);
return osObject << endl;
}
}
}
else
{
osObject << "That input is not valid." << endl;
return osObject;
}
}


When I ran the program again, the output was "The input is not valid."

So, it seems that the variables for number of rows and number of columns are not being initialized?

This is the code for my matrix class where I generate a random number to populate the cells of the matrices:

matrixType::matrixType(int numberOfRows, int numberOfColumns)
{
board = new int * [numberOfRows];
for (int i = 0; i < numberOfRows; i++)
{
board[i] = new int[numberOfColumns];
for (int j = 0; j < numberOfColumns; j++)
{
board[i][j] = rand() % 100;
}
}
}

My default constructor is the same as the above except that I initialize numberOfRows and numberOfColumns to 1.

This is my copy constructor:

matrixType::matrixType(const matrixType& otherMatrix)
{
numberOfRows = otherMatrix.numberOfRows;
numberOfColumns = otherMatrix.numberOfColumns;

for (int i=0; i < numberOfRows; i++)
{
for (int j = 0; j < numberOfColumns; j++)
{
board[i][j] = otherMatrix.board[i][j];
}
}
}

Any suggestions are appreciated!

Thanks,

Ragan
Topic archived. No new replies allowed.