0

Why am I getting linker errors when trying to compile this code, this is basically a code for a template class matrix that is complex & matrix is a square matrix so if size "3" is entered it means a matrix of [3][3] but somehow it gives me errors, any help?

#include <iostream>
#include <iomanip>
using namespace std;

template <class T>
class matrix
{
private:
  T** real;
  T** imag;
int size;
public:
  matrix(int = 0);
  friend ostream& operator<<(ostream& out, matrix<T>);
};

// constructor

template <class T>
matrix<T>::matrix(int length)
{
 size = length;

real = new T*[size];
for (int i = 0; i < size; i++)
    real[i] = new T[size];

imag = new T*[size];
for (int i = 0; i < size; i++)
    imag[i] = new T[size];

cout << "Enter real elements of matrix: ";
for (int i = 0; i < size; i++)
    for (int j = 0; j < size; j++)
        cin >> real[i][j];

cout << "Enter imag elements of matrix: ";
for (int i = 0; i < size; i++)
    for (int j = 0; j < size; j++)
        cin >> imag[i][j];
}

// functions defined here

template <class T>
ostream& operator<<(ostream& out, matrix<T> arg)
{
 out << showpos;
 for (int i = 0; i < arg.size; i++)
    for (int j = 0; j < arg.size; j++)
        out << arg.real[i][j] << arg.imag[i][j] << " ";
out << endl;
return out;
}

int main()
{
  matrix <int> obj1(3);
  cout << obj1;
}
valiano
  • 16,433
  • 7
  • 64
  • 79

2 Answers2

1

Because compiler was expecting non-template function.

friend ostream& operator<<(ostream& out, matrix<T>);

However you defined as

template <class T>
ostream& operator<<(ostream& out, matrix<T> arg)
{
    //some code
}

Make changes in the class definition as

template<class N> friend ostream& operator<<(ostream& out, matrix<T>);

Here is a link to template friend operators which provides nice explanation about using template friends.

EDIT 1:

Based on suggestion by vsoftco, you can also use the alternate way to define in-class:

class matrix{
//some code

//we don't need template here
friend ostream& operator<<(ostream& out, matrix<T>)
{
    out << showpos;
    for (int i = 0; i < arg.size; i++)
        for (int j = 0; j < arg.size; j++)
            out << arg.real[i][j] << arg.imag[i][j] << " ";
        out << endl;
    return out;
}
};
sam
  • 2,033
  • 2
  • 10
  • 13
  • The answer is technically correct, upvoted, however instead of `operator<<` as template (btw you'd need to change the name from `T`), you can provide the definition inside the class, without any need for `template`, like `ostream& operator<<(ostream& out, matrix arg){/*implementation here*/}`. Your function will then be visible due to ADL. – vsoftco May 20 '15 at 01:08
  • @vsoftco thanks for the suggestion. I edited my post but I am learning too so in case I made a mistake, let me know. ☺ – sam May 20 '15 at 01:24
  • In case you're interested why declaring the friend inline works and how ADL finds it: http://stackoverflow.com/questions/23721731/name-which-introduced-by-friend-declaration – vsoftco May 20 '15 at 01:32
  • @vsoftco thanks for the links. Came to answer and ended up learning new stuff. – sam May 20 '15 at 01:50
0

Thank you everyone for help. seems like i needed the template because it was a non-member function of class matrix, following is the correct code. Sorry if I've done anything wrong while posting this question because in-fact this is first time I'm using stack overflow.

#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

// Class !

template <class T> class matrix
{
private:
    T** real;
    T** imag;
    int size;
public:
    matrix(int = 0);
    matrix(int,int);
template <class T> friend ostream& operator<< <T>(ostream& out, matrix<T>);
matrix operator+(matrix);
matrix operator-(matrix);
};

// Constructor !

template <class T> matrix<T>::matrix(int lenght, int dummy)
{
size = lenght;

real = new T*[size];
for (int i = 0; i < size; i++)
    real[i] = new T[size];

imag = new T*[size];
for (int i = 0; i < size; i++)
    imag[i] = new T[size];
}

template <class T> matrix<T>::matrix(int length)
{
size = length;

real = new T*[size];
for (int i = 0; i < size; i++)
    real[i] = new T[size];

imag = new T*[size];
for (int i = 0; i < size; i++)
    imag[i] = new T[size];

cout << "Enter real elements of matrix >> \n";
for (int i = 0; i < size; i++)
    for (int j = 0; j < size; j++)
        cin >> real[i][j];

cout << "Enter imag elements of matrix: >> \n";
for (int i = 0; i < size; i++)
    for (int j = 0; j < size; j++)
        cin >> imag[i][j];
}

// Main()


int main()
{
int size;
cout << "Enter Size: ";
cin >> size;

cout << "\nMatrix A created !" << endl;
matrix <int> A(size);

cout << "\nMatrix B created !" << endl;
matrix <int> B(size);
system("cls");

cout << "Matrix A" << endl;
cout << A;

cout << "\nMatrix B" << endl;
cout << B;

cout << "\nMatrix A + B" << endl;
cout << A + B;

cout << "\nMatrix A - B" << endl;
cout << A - B;
}

// Functions !

template <class T> ostream& operator<<(ostream& out, matrix<T> arg)
{
out << showpos;
for (int i = 0; i < arg.size; i++)
{
    for (int j = 0; j < arg.size; j++)
    {
        out << arg.real[i][j] << arg.imag[i][j] << "i ";
    }
        out << endl;
}
return out;
}

template <class T> matrix<T> matrix<T>::operator+(matrix arg)
{
matrix<T> temp(size,0); // 0 is a inserted as dummy because I've overloaded             
constructor of class

for (int i = 0; i < size; i++)
{
    for (int j = 0; j < size; j++)
    {
        temp.real[i][j] = real[i][j] + arg.real[i][j];
        temp.imag[i][j] = imag[i][j] + arg.imag[i][j];
    }
}
return temp;
}

template <class T> matrix<T> matrix<T>::operator-(matrix arg)
{
matrix<T> temp(size, 0); // 0 is a inserted as dummy because I've overloaded  
constructor of class

for (int i = 0; i < size; i++)
{
    for (int j = 0; j < size; j++)
    {
        temp.real[i][j] = real[i][j] - arg.real[i][j];
        temp.imag[i][j] = imag[i][j] - arg.imag[i][j];
    }
}
return temp;
}