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)