Array of Pointers to Objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kareem Nutt

    #1

    Array of Pointers to Objects

    I'm fairly new to c++ coming from c, so I'm a little confused.

    I have the following:
    Page *pageTable;
    Desc *descTable;
    pageTable = new Page[100]; //array of Page objects
    descTable = new Desc[100]; //array of Desc objects

    Later on I have the following for loop:

    for(i=0;i<100;i ++)
    {
    if(descTable[i].valid==true)
    {
    (descTable[i].file)->writePage(page Table[i].pageNo,descTab le[i]);
    }
    }

    The Desc class has a (bool valid) and (int pageNo) and (File* file).
    The file method writePage is defined as:
    writePage(int pageNo, Page* pagePtr);

    When I try to compile this, I get the following error:
    error: no matching function for call to `File::writePag e(int&, Page&)'
    error: candidates are: const Status File::writePage (int, Page*)

    I just can't seem to figure out the correct way to pass around the
    pointers. If I need to change something I'd like it to be the code
    here, not the member functions of File.

    Anyone have any ideas? This has been driving me crazy! Thanks!
  • JKop

    #2
    Re: Array of Pointers to Objects

    [color=blue]
    > error: no matching function for call to `File::writePag e(int&, Page&)'
    > error: candidates are: const Status File::writePage (int, Page*)[/color]


    The second argument is of type "Page*", a pointer to a "Page" object.

    You're not giving it a pointer. Change:

    (descTable[i].file)->writePage( pageTable[i].pageNo, descTable[i] );

    to:

    (descTable[i].file)->writePage( pageTable[i].pageNo, &descTable[i] );


    -JKop

    Comment

    • John Harrison

      #3
      Re: Array of Pointers to Objects


      "JKop" <NULL@NULL.NULL > wrote in message
      news:PFc8d.3273 6$Z14.12008@new s.indigo.ie...[color=blue]
      >[color=green]
      > > error: no matching function for call to `File::writePag e(int&, Page&)'
      > > error: candidates are: const Status File::writePage (int, Page*)[/color]
      >
      >
      > The second argument is of type "Page*", a pointer to a "Page" object.
      >
      > You're not giving it a pointer. Change:
      >
      > (descTable[i].file)->writePage( pageTable[i].pageNo, descTable[i] );
      >
      > to:
      >
      > (descTable[i].file)->writePage( pageTable[i].pageNo, &descTable[i] );
      >[/color]

      Or this

      descTable[i].file->writePage( pageTable[i].pageNo, descTable + i );

      This isn't a C++ issue, you would have had exactly the same error in C.

      john


      Comment

      • Victor Bazarov

        #4
        Re: Array of Pointers to Objects

        Kareem Nutt wrote:[color=blue]
        > I'm fairly new to c++ coming from c, so I'm a little confused.
        >
        > I have the following:
        > Page *pageTable;
        > Desc *descTable;
        > pageTable = new Page[100]; //array of Page objects
        > descTable = new Desc[100]; //array of Desc objects
        >
        > Later on I have the following for loop:
        >
        > for(i=0;i<100;i ++)
        > {
        > if(descTable[i].valid==true)[/color]

        Just a nit-pick on your style: this could be (and would read a bit
        clearer):

        if (descTable[i].valid)
        [color=blue]
        > {
        > (descTable[i].file)->writePage(page Table[i].pageNo,descTab le[i]);[/color]
        ^^^^^^^^^^^
        Are you sure your actual code has this?[color=blue]
        > }
        > }
        >
        > The Desc class has a (bool valid) and (int pageNo) and (File* file).
        > The file method writePage is defined as:
        > writePage(int pageNo, Page* pagePtr);
        >
        > When I try to compile this, I get the following error:
        > error: no matching function for call to `File::writePag e(int&, Page&)'
        > error: candidates are: const Status File::writePage (int, Page*)
        >
        > I just can't seem to figure out the correct way to pass around the
        > pointers. If I need to change something I'd like it to be the code
        > here, not the member functions of File.
        >
        > Anyone have any ideas? This has been driving me crazy! Thanks![/color]

        'descTable[i]' is of type Desc. Your function needs Page*. It needs
        _an_address_ of what it is going to write, and you're providing the
        _value_ of some other type. Getting warmer? Can you do it yourself
        now? If not, here is the corrected statement:

        descTable[i].file->writePage(page Table[i].pageNo, &pageTable[i]);

        or

        descTable[i].file->writePage(page Table[i].pageNo, pageTable + i);

        (as you can see I removed the extraneous parentheses as well).

        Victor

        Comment

        • Victor Bazarov

          #5
          Re: Array of Pointers to Objects

          John Harrison wrote:[color=blue]
          > [..]
          > Or this
          >
          > descTable[i].file->writePage( pageTable[i].pageNo, descTable + i );[/color]
          ^^^^^^^^^
          pageTable.[color=blue]
          >
          > This isn't a C++ issue, you would have had exactly the same error in C.[/color]

          :-)

          Comment

          • John Harrison

            #6
            Re: Array of Pointers to Objects


            "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
            news:BUc8d.3934 $Ae.971@newsrea d1.dllstx09.us. to.verio.net...[color=blue]
            > John Harrison wrote:[color=green]
            > > [..]
            > > Or this
            > >
            > > descTable[i].file->writePage( pageTable[i].pageNo, descTable + i );[/color]
            > ^^^^^^^^^
            > pageTable.[color=green]
            > >
            > > This isn't a C++ issue, you would have had exactly the same error in C.[/color]
            >
            > :-)[/color]

            I must be being dense. I don't see it.

            john


            Comment

            • Victor Bazarov

              #7
              Re: Array of Pointers to Objects

              John Harrison wrote:[color=blue]
              > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
              > news:BUc8d.3934 $Ae.971@newsrea d1.dllstx09.us. to.verio.net...
              >[color=green]
              >>John Harrison wrote:
              >>[color=darkred]
              >>>[..]
              >>>Or this
              >>>
              >>>descTable[i].file->writePage( pageTable[i].pageNo, descTable + i );[/color]
              >>
              >> ^^^^^^^^^
              >> pageTable.
              >>[color=darkred]
              >>>This isn't a C++ issue, you would have had exactly the same error in C.[/color]
              >>
              >>:-)[/color]
              >
              >
              > I must be being dense. I don't see it.[/color]

              You don't see what? In the original post the second argument is said
              to be of type Page*, not Desc*. Reread it:
              [color=blue][color=green][color=darkred]
              >>>>> error: candidates are: const Status File::writePage (int, Page*)[/color][/color][/color]

              V

              Comment

              • Howard

                #8
                Re: Array of Pointers to Objects


                "Kareem Nutt" <methodmano@gma il.com> wrote in message
                news:V-6dncIhdt5ryfzcR Vn-pQ@comcast.com. ..[color=blue]
                > I'm fairly new to c++ coming from c, so I'm a little confused.
                >
                > I have the following:
                > Page *pageTable;
                > Desc *descTable;
                > pageTable = new Page[100]; //array of Page objects
                > descTable = new Desc[100]; //array of Desc objects
                >
                > Later on I have the following for loop:
                >
                > for(i=0;i<100;i ++)
                > {
                > if(descTable[i].valid==true)
                > {
                > (descTable[i].file)->writePage(page Table[i].pageNo,descTab le[i]);
                > }
                > }
                >
                > The Desc class has a (bool valid) and (int pageNo) and (File* file).
                > The file method writePage is defined as:
                > writePage(int pageNo, Page* pagePtr);
                >
                > When I try to compile this, I get the following error:
                > error: no matching function for call to `File::writePag e(int&, Page&)'
                > error: candidates are: const Status File::writePage (int, Page*)
                >
                > I just can't seem to figure out the correct way to pass around the
                > pointers. If I need to change something I'd like it to be the code
                > here, not the member functions of File.
                >[/color]

                You're passing a Desc (object) where a Page* (pointer) is expected. Others
                have suggested you pass &descTable[i], but that's a pointer to a Desc
                object, not a pointer to a Page object. I'm not sure which Page object you
                want, though. Is it the one at the same location [i] as the Desc object, or
                the one whose index is the same as the pageNo value of the Desc object? Or,
                is it the job of the writePage function to index into the pageTable array,
                using that index, to get the correct Page object?

                Without seeing the (relevant) contents of writePage, I can't tell what you
                really want. If writePage uses the pageNo to index into an array that
                you're passing it, then what you want is to pass pageTable, not
                descTable[i]. If what writePage expects is a pointer to a single Page
                object, then you want to pass it &pageTable[something], but again, what
                "something" is depends upon whether you're trying to write out the page at
                index [i] or the page whose index is the pageNo value from the Desc object.
                If the former, then you want to use [i]. If the latter, then you want to
                use [pageNo].

                There is another inconsistency in your code. You pass pageTable[i].pageNo
                as the page number int parameter, but you've stated that pageNo is a member
                of the Desc object! Given that, shouldn't the first parameter be
                descTable[i].pageNo?

                If you have the source for writePage, then you might want to post its code
                here, along with the two structures. That might help sort this out. (If
                writePage belongs to some library, then read the documentation on what the
                parameters should be.)

                -Howard

                -Howard






                Comment

                • John Harrison

                  #9
                  Re: Array of Pointers to Objects


                  "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
                  news:k6d8d.3935 $Ae.165@newsrea d1.dllstx09.us. to.verio.net...[color=blue]
                  > John Harrison wrote:[color=green]
                  > > "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message
                  > > news:BUc8d.3934 $Ae.971@newsrea d1.dllstx09.us. to.verio.net...
                  > >[color=darkred]
                  > >>John Harrison wrote:
                  > >>
                  > >>>[..]
                  > >>>Or this
                  > >>>
                  > >>>descTable[i].file->writePage( pageTable[i].pageNo, descTable + i );
                  > >>
                  > >> ^^^^^^^^^
                  > >> pageTable.
                  > >>
                  > >>>This isn't a C++ issue, you would have had exactly the same error in C.
                  > >>
                  > >>:-)[/color]
                  > >
                  > >
                  > > I must be being dense. I don't see it.[/color]
                  >
                  > You don't see what? In the original post the second argument is said
                  > to be of type Page*, not Desc*. Reread it:
                  >[color=green][color=darkred]
                  > >>>>> error: candidates are: const Status File::writePage (int, Page*)[/color][/color]
                  >[/color]

                  Right, I'm being dense.

                  john


                  Comment

                  • Victor Bazarov

                    #10
                    Re: Array of Pointers to Objects

                    Howard wrote:[color=blue]
                    > [..]
                    > If you have the source for writePage, then you might want to post its code
                    > here, along with the two structures. That might help sort this out. (If
                    > writePage belongs to some library, then read the documentation on what the
                    > parameters should be.)[/color]

                    You're absolutely correct. For whatever reason we assumed that what the
                    OP wrote was _almost_ there, except he passed by value instead of by
                    pointer. It could be that he just needed to pass the address of the very
                    first element of the array and the function will index it itself.

                    V

                    Comment

                    Working...