爲什麼我在嘗試編譯此代碼時收到鏈接器錯誤,這基本上是一個複雜的模板類矩陣的代碼&矩陣是一個方形矩陣,所以如果輸入大小「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