Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. C++: Function Pointer assignment cannot convert types
Forum Updated to NodeBB v4.3 + New Features

C++: Function Pointer assignment cannot convert types

Scheduled Pinned Locked Moved Solved General and Desktop
c++
6 Posts 3 Posters 3.9k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alex94102
    wrote on 13 Feb 2016, 18:31 last edited by Alex94102
    #1

    I am having issues properly declaring and using function pointers to functions defined inside a class:

    all works fine for use case outside a class, for example:

    typedef bool (*FunctionPointerType1)(int&);
    bool functionImplementation1(int& a) {
        a++;    // Just increments the parameter
    }
    FunctionPointerType1 getFunctionPointer1() {
        return functionImplementation1;
    }
    int main(int argc, char *argv[])
    {
        FunctionPointerType1 imp1 = getFunctionPointer1();
    

    Repeating the same exercise for a function defined inside a class I get to see the issue, why?

    class MainClass {
    private:
        typedef bool (MainClass::*FunctionPointerType)(int&);
        bool functionImplementation(int& a) {
        a++;    // Just increments the parameter
    }
    FunctionPointerType getFunctionPointer() {
        return functionImplementation;
        /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^
            error: cannot convert 'MainClass::functionImplementation' from type 'bool (MainClass::)(int&)' to type 'MainClass::FunctionPointerType {aka bool (MainClass::*)(int&)}'
        */
    }
    public:
    
    K 1 Reply Last reply 13 Feb 2016, 19:28
    0
    • A Alex94102
      13 Feb 2016, 18:31

      I am having issues properly declaring and using function pointers to functions defined inside a class:

      all works fine for use case outside a class, for example:

      typedef bool (*FunctionPointerType1)(int&);
      bool functionImplementation1(int& a) {
          a++;    // Just increments the parameter
      }
      FunctionPointerType1 getFunctionPointer1() {
          return functionImplementation1;
      }
      int main(int argc, char *argv[])
      {
          FunctionPointerType1 imp1 = getFunctionPointer1();
      

      Repeating the same exercise for a function defined inside a class I get to see the issue, why?

      class MainClass {
      private:
          typedef bool (MainClass::*FunctionPointerType)(int&);
          bool functionImplementation(int& a) {
          a++;    // Just increments the parameter
      }
      FunctionPointerType getFunctionPointer() {
          return functionImplementation;
          /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^
              error: cannot convert 'MainClass::functionImplementation' from type 'bool (MainClass::)(int&)' to type 'MainClass::FunctionPointerType {aka bool (MainClass::*)(int&)}'
          */
      }
      public:
      
      K Offline
      K Offline
      koahnig
      wrote on 13 Feb 2016, 19:28 last edited by
      #2

      @Alex94102

      I think this should be:

        typedef bool (MainClass::*FunctionPointerType)(int&);
      

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      1
      • A Offline
        A Offline
        Alex94102
        wrote on 13 Feb 2016, 19:55 last edited by
        #3

        Yes thanks, the post did't render the correctly, I re-posted that. That's exactly how it was to start with.

        K 1 Reply Last reply 13 Feb 2016, 20:01
        0
        • A Alex94102
          13 Feb 2016, 19:55

          Yes thanks, the post did't render the correctly, I re-posted that. That's exactly how it was to start with.

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 13 Feb 2016, 20:01 last edited by kshegunov
          #4

          @Alex94102

          FunctionPointerType getFunctionPointer()
          {
              return &MainClass::functionImplementation;
          }
          

          ?

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Alex94102
            wrote on 13 Feb 2016, 20:12 last edited by
            #5

            **I want to cry! Thank you soooooo much! I really appreciated your help. **

            K 1 Reply Last reply 13 Feb 2016, 21:19
            0
            • A Alex94102
              13 Feb 2016, 20:12

              **I want to cry! Thank you soooooo much! I really appreciated your help. **

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 13 Feb 2016, 21:19 last edited by kshegunov
              #6

              smiley

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0

              5/6

              13 Feb 2016, 20:12

              • Login

              • Login or register to search.
              5 out of 6
              • First post
                5/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved