0

The code compiles & runs just fine using Dev C++ but under VS2017 I get error C2276 at Get::Init(). I am a little bit surprised because this code was taken and isolated from the code https://hevc.hhi.fraunhofer.de/HM-doc/_t_com_rd_cost_8cpp_source.html at line 126 Void TComRdCost::init() that compiles just fine under VS2017. Where am I wrong here? What am I missing?

EDIT: setting the keyword static before static int xGetSSE16N( DistParam* pcDtParam ) solved the problem. Can someone explain why? And the question remains with Dev C++ that resolved the problem without static and VS2017 not?

error C2276 '&': illegal operation on bound member function expression"

#include <iostream>
using namespace std;

class DistParam;


typedef int (*FpDistFunc) (DistParam*); // int FpDistFunc(DistParam*)

class DistParam {
public:
    FpDistFunc CustomFunction; 
};

class Get {
public:     
    FpDistFunc m_afpDistortFunc[2];
    
    int xGetSSE16N( DistParam* pcDtParam ) {        
        std::cout << "xGetSSE16N is called \n\n";       
        return 1000;    
    }   
    void init() {       
        m_afpDistortFunc[0] = (FpDistFunc) (&xGetSSE16N);
    }
    void SetDistFunc(DistParam *p) {        
        p->CustomFunction = m_afpDistortFunc[0];        
    }   
};



int main() {
    
    DistParam m_cDistParam;
    Get a;
    
    a.init();
    a.SetDistFunc(&m_cDistParam);

    //execute the custom function
    int uiSad = m_cDistParam.CustomFunction( &m_cDistParam );
    
    std::cout << "uiSAD =" << uiSad;

    return 0;
}
Maverick
  • 1,105
  • 12
  • 41
  • The code you have posted has other errors beside the one you mentioned: the `init` and `SetDistFunc` member declarations have no types specified, and neither does `main`. But the issue here is really why does Dev C++ allow such a use of a non-static member function? – Adrian Mole Mar 09 '21 at 09:26
  • I'm guessing Dev C++ is compiling using gcc with the `-fpermissive` flag which allows non-standard code, without `-fpermissive` no c++ compiler should compile your code – Alan Birtles Mar 09 '21 at 09:28
  • @AdrianMole Can you please explain WHY the function should be static to compile? As far as I can understand the dev c++ correctly got the address of the function `xGetSSE16N`, I can not see any problem on this piece of code. – Maverick Mar 09 '21 at 09:40
  • When you call a non-static member function, there is an implied extra argument: the `this` pointer to the instance on which the function is being called. That extra argument (amongst other things) makes the function's signature different from that of the declared function pointer. See [Calling C++ class members via a function pointer](https://stackoverflow.com/q/1485983/10871073) – Adrian Mole Mar 09 '21 at 09:47
  • In your case, as the `xGetSSE16N` function doesn't actually reference any class members, then maybe the Dev C++ compiler is using some trickery to avoid the need for that `this` pointer ... but it's still not valid C++ code. – Adrian Mole Mar 09 '21 at 09:51
  • @AdrianMole Thank you, Adrian, I missed that. Is there any way to use the address of a nonstatic method in this case? PS : DevC++ warning using the way I attached above.`[Warning] converting from 'int (Get::*)(DistParam*)' to 'FpDistFunc {aka int (*)(DistParam*)}' [-Wpmf-conversions]` – Maverick Mar 09 '21 at 09:52
  • [This answer](https://stackoverflow.com/a/1486334/10871073) in the above-linked post may help. – Adrian Mole Mar 09 '21 at 09:54

0 Answers0