gpt4 book ai didi

使用夹具的 C++ GoogleTest - 函数指针定义不可访问

转载 作者:行者123 更新时间:2023-11-28 07:56:00 25 4
gpt4 key购买 nike

我实现了一个 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,它成功)。

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

最佳答案

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

class UnitTest_solver 中的所有内容都受到保护,因此所有内容(其他继承此类的类)都无法访问它的成员。改成public,你的问题就解决了:

class UnitTest_solver : public ::testing::Test
{

// protected:
public:

关于使用夹具的 C++ GoogleTest - 函数指针定义不可访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12723163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com