Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Invalid operands

Invalid operands

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 5 Posters 0 Views 1 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.
  • M Offline
    M Offline
    meerokh
    wrote on last edited by
    #1

    I am trying to calculate sum of fibonacci series. Struct arg is defined in header file as follows

    struct args {
    int number;
    int result;
    };

    args user_arguments = { 10, 0 };
    void fibonacci (void* arguments)
    {
    args* local_args = (args*) arguments;
    printf("local_args->number = %d\n", local_args->number);
    if (local_args->number <= 1) {
    local_args->result= 1;
    return;
    }
    int a = (local_args->number)-1;
    int b = (local_args->number)-2;
    local_args->result =fibonacci(&a) + fibonacci(&b)
    return local_args->result;

    Above function is called from the function below. createPackage is a function provided by another library which gonna execute fabonacci function.

    void WorkPackage(void*) {
    createPackage(fibonacci, &user_arguments);
    printf("Sum of fabonacci sequence is %d ", args->result) }

    While compiling, It gives the following error

    invalid operands of types ‘void’ and ‘void’ to binary ‘operator+’

    Please help in pointing what am i doing wrong here??

    V L C 3 Replies Last reply
    0
    • M meerokh

      I am trying to calculate sum of fibonacci series. Struct arg is defined in header file as follows

      struct args {
      int number;
      int result;
      };

      args user_arguments = { 10, 0 };
      void fibonacci (void* arguments)
      {
      args* local_args = (args*) arguments;
      printf("local_args->number = %d\n", local_args->number);
      if (local_args->number <= 1) {
      local_args->result= 1;
      return;
      }
      int a = (local_args->number)-1;
      int b = (local_args->number)-2;
      local_args->result =fibonacci(&a) + fibonacci(&b)
      return local_args->result;

      Above function is called from the function below. createPackage is a function provided by another library which gonna execute fabonacci function.

      void WorkPackage(void*) {
      createPackage(fibonacci, &user_arguments);
      printf("Sum of fabonacci sequence is %d ", args->result) }

      While compiling, It gives the following error

      invalid operands of types ‘void’ and ‘void’ to binary ‘operator+’

      Please help in pointing what am i doing wrong here??

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      You are trying to add two operands of the type void (fibonacci(&a)and fibonacci(&b)) :

      meerokh wrote:

      local_args->result =fibonacci(&a) + fibonacci(&b)

      and the compiler doesn't know how to do it!

      M 1 Reply Last reply
      0
      • V Victor Nijegorodov

        You are trying to add two operands of the type void (fibonacci(&a)and fibonacci(&b)) :

        meerokh wrote:

        local_args->result =fibonacci(&a) + fibonacci(&b)

        and the compiler doesn't know how to do it!

        M Offline
        M Offline
        meerokh
        wrote on last edited by
        #3

        How can i calculate the sum and add it back to struct variable??

        D 1 Reply Last reply
        0
        • M meerokh

          I am trying to calculate sum of fibonacci series. Struct arg is defined in header file as follows

          struct args {
          int number;
          int result;
          };

          args user_arguments = { 10, 0 };
          void fibonacci (void* arguments)
          {
          args* local_args = (args*) arguments;
          printf("local_args->number = %d\n", local_args->number);
          if (local_args->number <= 1) {
          local_args->result= 1;
          return;
          }
          int a = (local_args->number)-1;
          int b = (local_args->number)-2;
          local_args->result =fibonacci(&a) + fibonacci(&b)
          return local_args->result;

          Above function is called from the function below. createPackage is a function provided by another library which gonna execute fabonacci function.

          void WorkPackage(void*) {
          createPackage(fibonacci, &user_arguments);
          printf("Sum of fabonacci sequence is %d ", args->result) }

          While compiling, It gives the following error

          invalid operands of types ‘void’ and ‘void’ to binary ‘operator+’

          Please help in pointing what am i doing wrong here??

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Your definition of the fibonacci function is incorrect. You cannot return a value from a function defined as void, i.e. not returning anything. You also declare the arguments parameter as a void* but pass the address of an int variable to it. You then cast that to the address of an args structure, which will likely cause a segmentation fault. You need to rethink this code completely.

          1 Reply Last reply
          0
          • M meerokh

            How can i calculate the sum and add it back to struct variable??

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Have you tried having the fibonacci() function return a value, or if the function's signature is unchangeable, removing the return statement? You might also consider terminating the local_args->result = fibonacci(&a) + fibonacci(&b) statement with a semicolon.

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            1 Reply Last reply
            0
            • M meerokh

              I am trying to calculate sum of fibonacci series. Struct arg is defined in header file as follows

              struct args {
              int number;
              int result;
              };

              args user_arguments = { 10, 0 };
              void fibonacci (void* arguments)
              {
              args* local_args = (args*) arguments;
              printf("local_args->number = %d\n", local_args->number);
              if (local_args->number <= 1) {
              local_args->result= 1;
              return;
              }
              int a = (local_args->number)-1;
              int b = (local_args->number)-2;
              local_args->result =fibonacci(&a) + fibonacci(&b)
              return local_args->result;

              Above function is called from the function below. createPackage is a function provided by another library which gonna execute fabonacci function.

              void WorkPackage(void*) {
              createPackage(fibonacci, &user_arguments);
              printf("Sum of fabonacci sequence is %d ", args->result) }

              While compiling, It gives the following error

              invalid operands of types ‘void’ and ‘void’ to binary ‘operator+’

              Please help in pointing what am i doing wrong here??

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              You have to comply with the assigned interface, but such interface would probably make your Fibonacci series computation rather clumsy. I suggest you to separate the tasks: write a fibonacci function complying with the required interface wich, in turn, calls a trivial recursive implementation (say myfib) of the series computation:

              #include struct args
              {
              int number;
              int result;
              };

              // trivial recursive implementation on Fibonacci series computation
              static int myfib( int n )
              {
              if ( n < 3 )
              return 1;
              else
              return myfib(n-1) + myfib(n-2);
              }

              // the interface compliant function
              void fibonacci( void * arguments )
              {
              struct args * pargs = (struct args *) arguments;
              pargs->result = myfib( pargs->number); // call the private workhorse
              }

              // test program
              int main()
              {
              struct args a = { 10, 0 };
              fibonacci( &a );
              printf("fibonacci(%d)=%d\n", a.number, a.result);
              return 0;
              }

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups