C ++链接器错误,未解析的外部

时间:2015-05-19 23:44:49

标签: c++ templates linker-errors lnk2019

为什么在尝试编译此代码时会出现链接器错误,这基本上是一个复杂的模板类矩阵的代码。矩阵是一个方阵,所以如果输入大小为“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;
}

2 个答案:

答案 0 :(得分:1)

因为编译器期待非模板功能。

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>);

Here是指向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;
}
};

答案 1 :(得分:0)

谢谢大家的帮助。似乎我需要模板,因为它是类矩阵的非成员函数,以下是正确的代码。很抱歉,如果我在发布此问题时做错了什么,实际上这是我第一次使用堆栈溢出。

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