0

我用fixture class 实现了一个googletest UnitTest_solver。夹具的实现如下。它包含辅助函数

class UnitTest_solver : public ::testing::Test
{

protected:

    static void SetUpTestCase()
    {
        // setup table with data
        m_col = 2; 
        m_row = 100;
        //  other things to initialize m_test_data 
    }



    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)
    {
        if(m_col < 2)
            return 0;

        double fx = 0;

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

        for(int i = 0 ; i < m_row ; i++)
        {
            for(int j = 0 ; j < m_col - 1 ; j++)
                row_i[j] = m_test_data[i][j];

            fx += pow(m_test_data[i][0] - func_1(x, row_i, m_col - 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 UnitTest_solver::functPtr( double * parameters, void * userinfo)
    {
        return  chi_sqr(parameters);
    }

    static ofstream thing;
    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;

double (UnitTest_solver::*functPtr)(double * , void *) = 0;

然后,我有一个测试用例,带有指向夹具 UnitTest_solver 的链接。

TEST_F(UnitTest_solver, testFunc1)
{
    Solver* solversqp = new Solver();
    solversqp->set_pointer_to_function(UnitTest_solver::functPtr, (void*)0);
    //...
}

第二行显示编译时错误UnitTest_solver::functPtr:当鼠标悬停在错误上时,信息是“在第 xxx 行定义的函数无法访问”,xxx 指向functPtr夹具内的定义。

如果我运行 ggltest 并评论最后一行,solversqp->set_pointer_to_function(UnitTest_solver::functPtr, (void*)0);则测试正在完成(如果我放了一个简单的 ASSERT,它是成功的)。

我的函数指针定义有什么问题。

4

1 回答 1

1

我没有看到完整的代码,因此这只是一个猜测。

中的所有class UnitTest_solver内容都受到保护,因此所有内容(为此类继承的其他类)都无权访问它的成员。将其更改为公开,您的问题将得到解决:

class UnitTest_solver : public ::testing::Test
{

// protected: 
public:
于 2012-10-04T09:33:30.020 回答