kiriloff
kiriloff

Reputation: 26335

C++ GoogleTest using fixture - function pointer definition not accessible

I implemented a googletest, with fixture class UnitTest_solver. Implementation for the fixture is the following. It contains helper functions

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;

 };

Also, out of the fixture scope, i initialize static variables. Last is function pointer. is definition syntax ok ?

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;

then, i have a test case, with link to fixture UnitTest_solver.

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

second line show compile time error with UnitTest_solver::functPtr : when mouse is over the error, info is 'function defined at line xxx is unacessible', with xxx pointing to functPtr definition inside the fixture.

If i run the ggltest commenting the last line, solversqp->set_pointer_to_function(UnitTest_solver::functPtr, (void*)0);, test is finishing (if i put a trivial ASSERT, it is successful).

Whats wrong with my function pointer definition.

Upvotes: 0

Views: 2628

Answers (1)

BЈовић
BЈовић

Reputation: 64213

I do not see full code, therefore this is just a guess.

Everything in class UnitTest_solver is protected, therefore everything (other then classes inheriting for this class) do not have access to it's members. Change it to public, and your problem will be solved :

class UnitTest_solver : public ::testing::Test
{

// protected: 
public:

Upvotes: 1

Related Questions