2012-10-03 51 views
0

我有這個代碼示例,在googletest中定義一個fixture。C++ GoogleTest - 靜態成員和鏈接器錯誤LNK 2001

#include "solver.h" 

#include <math.h> 
#include <iostream> 
#include <fstream> 
#include <string> 

using namespace std; 


class UnitTest_solver : public ::testing::Test 
{ 
protected: 

    static void SetUpTestCase() 
    { 
     // setup table with data 
     m_col = 2; 
     m_test_data = new double*[m_row]; 
     for(int i = 0 ; i < m_col ; i++) 
      m_test_data[i] = new double[m_row]; 

     ifstream data_x; 
     data_x.open("exponential2X.txt"); 

     // etc... : i setup table m_test_data in a few more lines 
} 



static void TearDownTestCase() 
{ 
    for(int i = 0 ; i < m_row ; i++) 
     delete[] m_test_data[i]; 
    delete[] m_test_data; 
} 



static double chi_sqr(double* x, double(*func)(double*, double*, int)) 
{ 
    if(ncol < 2) 
     return; 

    double fx = 0; 

    double * row_i = new double[ncol - 1]; 

    for(int i = 0 ; i < nrow ; i++) 
    { 
     for(int j = 0 ; j < ncol - 1 ; j++) 
      row_i[j] = data[i][j]; 

     fx += pow(data[i][0] - (*func)(x, row_i, ncol - 1), 2.0); 
    } 

    return fx; 
} 


static double func_1(double* x, double* dbl, int nb_param) 
{ 
    if(nb_param != 2) 
     return 0; 

    return x[0] * exp(-1 * x[1] * dbl[0]); 
} 


static double warpPt2apply_func(double * parameters, int ncol, void * userinfo, int nb_p) 
{ 
     return chi_sqr(parameters, (ncol - 1), (*func_1)); 
} 

    static double** m_test_data; 
    static int m_col, m_row; 

}; 


TEST_F(UnitTest_solver, testFunc1) 
{ 
    Solver* solver = new Solver(); 
    solver->set_pointer(warpPt2apply_func, (void*)NULL); 
} 

我有LNK 2001錯誤,problably與使用靜態數據成員的,指向成員m_test_datam_colm_row。是因爲在開始時爲測試套件中的每個新測試調用了SetUpTestCase()方法,而靜態成員應該一勞永逸地初始化。

有什麼想法?

感謝

+0

是的,我有一個想法。顯示使用確切的錯誤。 –

回答

2
static double** m_test_data; 
    static int m_col, m_row; 

初始化類以外的封閉命名空間

double** UnitTest_solver::m_test_data = 0; 
    int UnitTest_solver::m_col = 0; 
    int UnitTest_solver::m_row = 0;