2015-05-19 81 views
0

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

你打算髮布錯誤嗎? –

+1

我的水晶球(因爲沒有發佈錯誤)說這個模板是在與main()分開的cpp文件中實現的。 – drescherjm

回答

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

Heretemplate 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; 
} 
}; 
+0

答案在技術上是正確的,upvoted,但不是'operator <<'作爲模板(順便說一句,你需要從'T'改變名稱),你可以在類中提供定義,而不需要'template ',就像'ostream&operator <<(ostream&out,matrix arg){/ * here here * /}'。由於ADL,您的功能將可見。 – vsoftco

+0

@vsoftco感謝您的建議。我編輯了我的帖子,但我也在學習,以免萬一我犯了一個錯誤,請告訴我。 ☺ – sam

+0

如果您有興趣爲什麼要聲明朋友內聯工作以及ADL如何找到它:http://stackoverflow.com/questions/23721731/name-which-introduced-by-friend-declaration – vsoftco

相關問題