相关疑难解决方法(0)

如何将类成员函数作为回调传递?

我正在使用一个API,要求我将函数指针作为回调传递.我正在尝试从我的类中使用此API,但是我遇到了编译错误.

这是我从构造函数中做的:

m_cRedundencyManager->Init(this->RedundencyManagerCallBack);
Run Code Online (Sandbox Code Playgroud)

这不编译 - 我收到以下错误:

错误8错误C3867:'CLoggersInfra :: RedundencyManagerCallBack':函数调用缺少参数列表; 使用'&CLoggersInfra :: RedundencyManagerCallBack'创建指向成员的指针

我尝试使用这个建议&CLoggersInfra::RedundencyManagerCallBack- 对我不起作用.

对此有何建议/解释?

我正在使用VS2008.

谢谢!!

c++ function-pointers callback c++03

66
推荐指数
4
解决办法
10万
查看次数

使用成员函数作为比较器排序问题

试图编译以下代码我得到这个编译错误,我该怎么办?


ISO C++禁止获取非限定或带括号的非静态成员函数的地址,以形成指向成员函数的指针.

class MyClass {
   int * arr;
   // other member variables
   MyClass() { arr = new int[someSize]; }

   doCompare( const int & i1, const int & i2 ) { // use some member variables } 

   doSort() { std::sort(arr,arr+someSize, &doCompare); }

}; 
Run Code Online (Sandbox Code Playgroud)

c++ sorting stl compiler-errors

30
推荐指数
4
解决办法
3万
查看次数

如何在C++中使用自定义排序成员函数的sort()?

我有一个关于将比较函数传递给的问题sort().

我想要做的是定义一个sort()函数,该函数在计算时考虑了我想要进行排序的类的成员变量.

基本上,我的代码看起来像这样(简化为只显示相关部分):

MappingTechnique.h

struct MappingTechnique {
    vector<int> usedIndexCount; 
};

struct SimpleGreedyMappingTechnique : MappingTechnique {
    bool sortByWeights(int index1, int index2);
};
Run Code Online (Sandbox Code Playgroud)

MappingTechnique.m

bool SimpleGreedyMappingTechnique::sortByWeights(int index1, int index2) {
    return usedIndexCount[index1] > usedIndexCount[index2];
}

void SimpleGreedyMappingTechnique::processFrame(Frame frame) {
    vector<int> payloadIndices = <generate the vector>

    // sort the payload indices according to their current usedIndexCount
    sort(payloadIndices.begin(), payloadIndices.end(), sortByWeights);
}
Run Code Online (Sandbox Code Playgroud)

此代码无法编译,它会出现以下错误:

 error: reference to non-static member function must be called
Run Code Online (Sandbox Code Playgroud)

并指出sortByWeights.

甚至可以使用类的成员函数进行排序吗?如果是,我该如何实现呢?

c++ sorting functor

3
推荐指数
1
解决办法
1044
查看次数