2014-03-27 43 views
1

我有以下代碼,頭文件和源代碼,我正嘗試構建dll和lib以用於其他項目。我覺得我已經按照正確的步驟導出了dll和lib,但是lib文件並沒有顯示出來。我已經檢查了其他人的問題,並將EXPORTS添加到了預處理器定義中,其他人表示解決了他們的問題,但不是我的。當導出存在時,導入庫不會構建

Matrix.h

#ifdef MATRIX_EXPORTS 
#define MATRIX_API __declspec(dllexport) 
#else 
#define MATRIX_API __declspec(dllimport) 
#endif 

#include <vector> 

using namespace std; 

template<typename T> class MATRIX_API Matrix 
{ 
public: 
typedef T value_type; 
~Matrix(); 
Matrix(); 
Matrix(int rows, int columns); 
int height; 
int width; 
int stride; 
size_t size; 

T &GetElement(int row, int column); 
void SetElement(int row, int column, T value); 
void SetElements(vector<T> value); 
vector<T>& GetElements(); 
T* GetPointer(); 
void Transpose(Matrix<T> &aMatrix); 
void Pivot(Matrix<T>&A, Matrix<T>&P); 
void LU_Decomp(Matrix<T>&A, Matrix<T>&L, Matrix<T>& U, Matrix<T>& P); 
bool isSingular(Matrix<T>&L, Matrix<T>&U); 
void CreateIdentity(Matrix<T>&I);*/ 
private: 
vector<T> elements; 
T* firstElement; 
}; 

Matrix.cpp

#include "Matrix.h" 
#include <vector> 
#include <iostream> 

using namespace std; 

template<typename T> 
Matrix<T>::~Matrix() 
{ 
} 

template<typename T> 
Matrix<T>::Matrix() 
{ 
} 

template<typename T> 
Matrix<T>::Matrix(int rows, int columns) 
{ 
height = rows; 
width = columns; 
stride = columns; //in row major order this is equal to the # of columns 
elements.resize(rows*columns); 
firstElement = elements.data(); 
size = height*width*sizeof(T); 
} 

template<typename T> 
MATRIX_API T &Matrix<T>::GetElement(int row, int column) 
{ 
return elements[row*width + column]; //row major order return 
} 

template<typename T> 
MATRIX_API vector<T>& Matrix<T>::GetElements() 
{ 
return elements; //row major order return 
} 

template<typename T> 
MATRIX_API void Matrix<T>::SetElement(int row, int column, T value) 
{ 
elements[row*width + column] = value; //row major order return 
} 

template<typename T> 
MATRIX_API void Matrix<T>::SetElements(vector<T> value) 
{ 
elements = value; 
} 

template<typename T> 
MATRIX_API T* Matrix<T>::GetPointer() 
{ 
return firstElement; 
} 

template<typename T> 
MATRIX_API void Matrix<T>::Transpose(Matrix<T> &aMatrix) 
{ 
Matrix<T> theTranspose(aMatrix.width,aMatrix.height); 

for (int i = 0; i < theTranspose.height; i++) 
{ 
    for (int j = 0; j < theTranspose.width; j++) 
    { 
     theTranspose.SetElement(i,j,aMatrix.GetElement(j,i)); 
    } 
} 

*this = theTranspose; 
} 


template<typename T> 
//Calculates C=A*B 
MATRIX_API Matrix<T> operator * (Matrix<T> A, Matrix<T> B) 
{ 
Matrix<T> C(A.height, B.width); 

for (int m = 0; m < C.height; m++) 
{ 
    for (int p = 0; p < C.width; p++) 
    { 
     T value = (T)0; 
     for (int n = 0; n < A.width; n++) 
     { 
      value += A.GetElement(m,n)*B.GetElement(n,p); 
      C.SetElement(m,p,value); 
     } 
    } 
} 

return C; 
} 

template<typename T> 
MATRIX_API Matrix<T> operator + (Matrix<T> &A, Matrix<T> &B) 
{ 
if (A.height != B.height || A.width != B.width) 
{ 
    cout<<" \n can't add these two matrices. They are not of equal dimensions.   Program Quitting"<<endl; 
} 

Matrix<T> sum(A.height, A.width); 
T value = NULL; 

for (int i = 0; i < A.height; i++) 
{ 
    for (int j = 0; j < A.width; j++) 
    { 
     value = A.GetElement(i,j) + B.GetElement(i,j); 
     sum.SetElement(i,j, value); 
    } 
} 

return sum; 
} 

template<typename T> 
MATRIX_API Matrix<T> operator += (Matrix<T> &A, Matrix<T> &B) 
{ 
A = A + B; 

return A; 
} 

template<typename T> 
MATRIX_API void Matrix<T>::Pivot(Matrix<T>&A, Matrix<T>&P) 
{ 
for (int i = 0; i < A.height; i++) 
{ 
    for (int j = 0; j < A.height; j++) 
    { 
     P.SetElement(i,j,i==j); 
    } 
} 

for (int i = 0; i < A.height; i++) 
{ 
    int max_j = i; 

    for (int j = i; j < A.height; j++) 
    { 
     if (fabs(A.GetElement(j,i)) > fabs(A.GetElement(max_j, i))) 
     { 
      max_j = j; 
     } 
    } 

    if (max_j != i) 
    { 
     for (int k = 0; k < A.height; k++) 
     { 
      T temp = P.GetElement(i,k); 
      P.SetElement(i,k,P.GetElement(max_j,k)); 
      P.SetElement(max_j,k,temp); 
     } 
    } 
} 
} 

template<typename T> 
MATRIX_API void Matrix<T>::LU_Decomp(Matrix<T>&A, Matrix<T>&L, Matrix<T>& U, Matrix<T>& P) 
{ 
int size = L.height; 
pivot(A,P); 

Matrix<T> Aprime; 
Aprime = Aprime.prod(P,A); 

for (int i = 0; i < size; i++) 
{ 
    L.SetElement(i,i,1); 
} 

for (int i = 0; i < size; i++) 
{ 
    for (int j = 0; j < size; j++) 
    { 
     T s; 

     if (j <= i) 
     { 
      s = 0; 
      for (int k = 0; k < j; k++) 
      { 
       s+=L.GetElement(j,k)*U.GetElement(k,i); 
      } 

      T value = Aprime.GetElement(j,i) - s; 
      U.SetElement(j,i,value); 
     } 

     if (j >= i) 
     { 
      s = 0; 
      for (int k = 0; k < i; k++) 
      { 
       s+= L.GetElement(j,k)*U.GetElement(k,i); 
      } 

      T value = (Aprime.GetElement(j,i) - s)/U.GetElement(i,i); 
      L.SetElement(j,i,value); 
     } 
    } 
} 
} 

template<typename T> 
//Take determinant of L and U matrices of LU decomposition to determine if parent matrix is singular 
MATRIX_API bool Matrix<T>::isSingular(Matrix<T>&L, Matrix<T>&U) 
{ 
T detL = L.GetElement(0,0); 
T detU = U.GetElement(0,0); 

for (int i = 1; i < L.height; i++) 
{ 
    detL = detL*L.GetElement(i,i); 
    detU = detU*L.GetElement(i,i); 
} 

if (detL*detU == 0) 
{ 
    return true; 
} 
else 
{ 
    return false; 
} 
} 
+0

請參閱http://stackoverflow.com/questions/495021/why-can-templates只在頭文件中實現 – SleuthEye

回答

1

也許不是問題,但你應該添加MATRIX_EXPORTS不能出口到預處理器定義。此外,您需要在庫中實例化您的模板,否則不會出現導出(假設您不會導出除您的模板類之外的其他任何內容)

+0

是的,當我說出口我的意思是MATRIX_EXPORTS。我也把所有的東西從上面的頭文件移到了cpp中。任何包括我擁有的和#ifdef MATRIX_EXPORTS等我保存在頭文件中。它仍然沒有建立一個庫 – user3390212

+0

你確定你正在實例化庫中的模板嗎?如果你還沒有解決,給我發送代碼,我會解決它:-) –

相關問題