0

Class:

#include <string>
using namespace std;

class Matrix{
public:
float **grid;
Matrix(int r, int c);
friend istream& operator >>(istream& cin, Matrix & m );
void setMatrix();
void printMatrix();
friend ostream& operator <<(ostream& cout, Matrix & m);
private:
int row;
int column;

};

istream& operator >>(istream& cin, Matrix & m );
ostream& operator <<(ostream& cout, Matrix & m);

Matrix cpp

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

//First constructor
Matrix::Matrix(int r, int c){
row=r; // Row size
column=c; // Column Size
if(row>0 && column>0)
{
    grid = new float*[row]; // Creating 2d dynamic array
     for(int i=0; i<row;i++)
    grid[row] = new float [column];

    //Setting all elements to 0
    for(int i=0; i<row; i++)
    {
        for(int j =0; j<column; j++)
        {
            grid[i][j]=0;
        }


    }

}
else{
    cout<<"Invalid number of rows or columns!"<<endl;

}


}

//Setting values to the matrix

void Matrix::setMatrix()
{

for(int i=0; i<row; i++)
{
    for(int j=0;j<column;j++)
    {
        cin>>grid[i][j];
    }
}

}

//Print matrix
void Matrix::printMatrix()
{

for(int i=0;i<row;i++)
{
    for(int j=0; j<column; j++)
    {
        cout<<grid[i][j]<<" ";
    }

    cout<<endl;

}
}

//Istream function
istream& operator >>(istream& cin, Matrix & m)
{
    m.setMatrix();
    return cin;
}

//ostream function
ostream& operator>>(ostream& cout, Matrix & m)
{
    m.printMatrix();
    return cout;

}

Main function

#include <iostream>
#include "Matrix.h"

using namespace std;

int main()
{
  Matrix m = Matrix(3,2);
  cout << m;
  return 0;
}

I'm trying to write a program to carry out different matrix operations.This segment of the code should mainly create a matrix of dimensions 3*2 and the constructor initialises all it's values to 0 and the matrix is printed. On compiling this, I'm getting a "linker command failed with exit 1" error which I'm really not sure how to fix. How could I go about fixing the same?

Error:

Undefined symbols for architecture x86_64: "operator<<(std::__1::basic_ostream >&, Matrix&)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

  • Copy the full and complete output when building, as text, and *edit your question* to show it. And please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [**Minimal**, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 28 '17 at 09:14
  • The output operator signature should be `ostream& operator <<(ostream& cout, const Matrix & m);` Also naming the parameter `cout` isn't a good idea. How to solve linker errors yous should read [this](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). – user0042 Oct 28 '17 at 09:15
  • Copy the error. Paste it in your question. Place it in a quote with 4 extra spaces after `>`. Oh and you have two `>>` implementations; no idea if that is a transcription error or what. – Yakk - Adam Nevraumont Oct 28 '17 at 09:16
  • Also never do `using namespace std;` in a header file! It's [bad practice in general](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?noredirect=1&lq=1) but even worse in a header file. – Some programmer dude Oct 28 '17 at 09:16
  • Don’t use new, don’t use an array of pointers to arrays. Simply use `std::vector grid(row*column)`. –  Oct 28 '17 at 10:07

2 Answers2

0

You have typo error, change one of operator >>

ostream& operator>>(ostream& cout, Matrix & m)
{
   m.printMatrix();
   return cout;
}

to

ostream& operator<<(ostream& cout, Matrix & m) {
   m.printMatrix();
   return cout;

}
Varun
  • 447
  • 4
  • 9
0

And also

    grid = new float*[row]; // Creating 2d dynamic array
    for (int i = 0; i<row; i++)
        grid[row] = new float[column];

should be

    grid = new float*[row]; // Creating 2d dynamic array
    for (int i = 0; i<row; i++)
        grid[i] = new float[column];
VPK
  • 29
  • 3