Ошибки компоновщика C++, неразрешенные внешние

Почему я получаю ошибки компоновщика при попытке скомпилировать этот код, это в основном код для сложной матрицы класса шаблона, а матрица представляет собой квадратную матрицу, поэтому, если введен размер «3», это означает, что матрица [3] [3] ] но почему-то это дает мне ошибки, любая помощь?

#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;
}

person Waleed Ahmed    schedule 19.05.2015    source источник
comment
ты собирался публиковать ошибки?   -  person Red Alert    schedule 20.05.2015
comment
Мой хрустальный шар (поскольку ошибок не было опубликовано) говорит, что шаблон был реализован в файле cpp отдельно от main().   -  person drescherjm    schedule 20.05.2015


Ответы (2)


Потому что компилятор ожидал нешаблонной функции.

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

Однако вы определили как

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

Внесите изменения в определение класса как

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

Вот ссылка на template friend operators, в которой дается хорошее объяснение использования шаблонов друзей.

РЕДАКТИРОВАТЬ 1:

По предложению vsoftco вы также можете использовать альтернативный способ определения в классе:

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;
}
};
person sam    schedule 20.05.2015
comment
Ответ технически правильный, за него проголосовали, однако вместо operator<< в качестве шаблона (кстати, вам нужно изменить имя с T) вы можете предоставить определение внутри класса без необходимости использования template, например ostream& operator<<(ostream& out, matrix<T> arg){/*implementation here*/}. Ваша функция будет видна благодаря ADL. - person vsoftco; 20.05.2015
comment
@vsoftco спасибо за предложение. Я отредактировал свой пост, но я тоже учусь, поэтому, если я допустил ошибку, дайте мне знать. ☺ - person sam; 20.05.2015
comment
Если вам интересно, почему работает объявление встроенного друга и как его находит ADL: stackoverflow.com/questions/23721731/ - person vsoftco; 20.05.2015
comment
@vsoftco спасибо за ссылки. Пришел, чтобы ответить, и в итоге узнал что-то новое. - person sam; 20.05.2015

Спасибо всем за помощь. похоже, мне нужен был шаблон, потому что это была функция, не являющаяся членом матрицы классов, ниже приведен правильный код. Извините, если я сделал что-то не так при публикации этого вопроса, потому что на самом деле я впервые использую переполнение стека.

#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;
}
person Waleed Ahmed    schedule 22.05.2015