Array of Pointers to Objects

K

Kareem Nutt

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.valid==true)
{
(descTable.file)->writePage(pageTable.pageNo,descTable);
}
}

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::writePage(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!
 
J

JKop

error: no matching function for call to `File::writePage(int&, Page&)'
error: candidates are: const Status File::writePage(int, Page*)


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

You're not giving it a pointer. Change:

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

to:

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


-JKop
 
V

Victor Bazarov

Kareem said:
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.valid==true)


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

if (descTable.valid)
{
(descTable.file)->writePage(pageTable.pageNo,descTable);

^^^^^^^^^^^
Are you sure your actual code has this?
}
}

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::writePage(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!

'descTable' 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.file->writePage(pageTable.pageNo, &pageTable);

or

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

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

Victor
 
J

John Harrison

JKop said:
error: no matching function for call to `File::writePage(int&, Page&)'
error: candidates are: const Status File::writePage(int, Page*)


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

You're not giving it a pointer. Change:

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

to:

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


Or this

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

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

john
 
V

Victor Bazarov

John said:
[..]
Or this

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

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


:)
 
J

John Harrison

Victor Bazarov said:
John said:
[..]
Or this

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

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


:)


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

john
 
V

Victor Bazarov

John said:
John said:
[..]
Or this

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

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


:)



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


You don't see what? In the original post the second argument is said
to be of type Page*, not Desc*. Reread it:

V
 
H

Howard

Kareem Nutt said:
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.valid==true)
{
(descTable.file)->writePage(pageTable.pageNo,descTable);
}
}

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::writePage(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.


You're passing a Desc (object) where a Page* (pointer) is expected. Others
have suggested you pass &descTable, 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 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. 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 or the page whose index is the pageNo value from the Desc object.
If the former, then you want to use . If the latter, then you want to
use [pageNo].

There is another inconsistency in your code. You pass pageTable.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.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
 
J

John Harrison

Victor Bazarov said:
John said:
John Harrison wrote:

[..]
Or this

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

^^^^^^^^^
pageTable.

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

:)



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


You don't see what? In the original post the second argument is said
to be of type Page*, not Desc*. Reread it:


Right, I'm being dense.

john
 
V

Victor Bazarov

Howard said:
[..]
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.)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top