Pointer questions - GNU c++ on a unix platform

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emp
    New Member
    • Nov 2006
    • 32

    Pointer questions - GNU c++ on a unix platform

    I have 3 structures in my header file
    struct{
    int a;
    int b;
    } header;

    struct{
    int c;
    int d;
    } trailer;

    struct{
    header hdr;
    trailer trl;
    } msg_struct;

    I declare an instance in my code

    msg_struct new_msg;

    After I populate new_msg, I then want to pass a pointer to new message to a special printFunc() I have written.

    My call to the function
    printFunc(&new_ msg);

    HERE's My first problem -----
    What should the function prototype look like in my header file for printFunc()
    I have
    printFunc(int *)

    I get compiler error saying cannot convert int** to int* for argument 1.....

    HERE's My second problem -----

    In printFunc(), to cout the contents of new_msg, should the line read
    std::cout<< new_msg->header.a << std::endl;
    std::cout<< new_msg->header.b << std::endl;
    etc........

    This gives compiler "error expected unqualified-id before '.' token" which I assume means that it doesn't understand what the "new_msg->" is

    Any help would be appreciated.

    Thanks

    emp1953
  • mschenkelberg
    New Member
    • Jun 2007
    • 44

    #2
    Change your printFunc( int * ) function to printFunc ( msg_struct * new_msg )

    that should solve the problems

    Max

    Comment

    • emp
      New Member
      • Nov 2006
      • 32

      #3
      I assume you suggest making the change both in the function declaration in the header file and in the function itself?

      When I do that I get a compiler error that says something very similar to original error.

      cannot convert msg_struct** to msg_struct*
      and the unqualified-id error has not changed

      Comment

      • mschenkelberg
        New Member
        • Jun 2007
        • 44

        #4
        can you post all of the source?

        Comment

        • emp
          New Member
          • Nov 2006
          • 32

          #5
          What I'm working is sensitive so I'm posting a simplified version
          Here's the header file

          printFunc.h

          struct{
          int a;
          int b;
          } header;

          struct{
          int c;
          ind d;
          } trailer;

          struct{

          header hdr;

          trailer trl;

          } msg_struct;





          void decodeOutput(ms g_type_enum, msg_struct *);
          void printFunc(msg_s truct *);



          ############### ############### ############### ############### ############### #######





          Here's the code.cpp

          #include "printFunc. h"

          void decodeOutput(ms g_type_enum typeIn, msg_struct *msgIn){
          {
          switch(typeIn)

          {

          case 0: printFunc(&msgI n);

          default: break;

          }

          }
          void printFunc(msg_s truct *msgIn)

          {

          std::cout<< msgin->hdr.a << std::endl;

          std::cout<< msgin->hdr.b << std::endl;

          std::cout<< msgin->trl.c << std::endl;

          std::cout<< msgin->trl.d << std::endl;

          }

          ############### ############### ############### ############### ############### ########


          Compiler errors


          error: cannot convert int** to int* for argument 1 (""this refers to the case 0 call to printFunc in the switch")

          error: expected unqualified-id before '.' token (""This refers to the couts, referring to the msgIn->")

          Comment

          • emp
            New Member
            • Nov 2006
            • 32

            #6
            Sorry, a correction to my most recent code pose

            error: cannot convert msg_struct** to msg_struct* for argument 1 (""this refers to the case 0 call to printFunc in the switch")

            error: expected unqualified-id before '.' token (""This refers to the couts, referring to the msgIn->")

            Comment

            • mschenkelberg
              New Member
              • Jun 2007
              • 44

              #7
              It is because you already passed the msg_struct as a pointer to the decode function therefore you do not need to & operator when passing the msg_struct to the printFunc. It should look like this:

              case 0:
              printFunc( msgln );


              Max

              Comment

              • emp
                New Member
                • Nov 2006
                • 32

                #8
                Originally posted by mschenkelberg
                It is because you already passed the msg_struct as a pointer to the decode function therefore you do not need to & operator when passing the msg_struct to the printFunc. It should look like this:

                case 0:
                printFunc( msgln );


                Max
                ############### ############### ############### #######

                Thank You Max, that got rid of the can't convert msg_struct** to msg_struct* error

                I still have the compiler error
                expected unqualified-id before '.' token

                Which I assume is the msgIn-> in my cout....


                emp1953

                Comment

                • mschenkelberg
                  New Member
                  • Jun 2007
                  • 44

                  #9
                  you typed msgin -> ... when it should be msgln

                  capital vs lowercase

                  Max

                  Comment

                  • emp
                    New Member
                    • Nov 2006
                    • 32

                    #10
                    Originally posted by mschenkelberg
                    you typed msgin -> ... when it should be msgln

                    capital vs lowercase

                    Max


                    Duh! My brain is on caffeine overdose. I hope I don't have to bother you guys with this any more

                    Thanks again

                    Comment

                    Working...