为什么我在尝试编译此代码时收到链接器错误,这基本上是一个复杂的模板类矩阵的代码&矩阵是一个方形矩阵,所以如果输入大小“3”意味着[3] [3]的矩阵,但不知怎的,它给了我错误,有什么帮助?C++链接器错误,无法解析的外部
#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;
}
你打算发布错误吗? –
我的水晶球(因为没有发布错误)说这个模板是在与main()分开的cpp文件中实现的。 – drescherjm