About default copy constructor when using auto_ptr as the member

P

PengYu.UT

I have the following program. The auto_ptr cause the compile error.
When I change auto_ptr to pointer, the error gone.

Would you please help me to understand what is wrong?

/*******************************************/
class iterator {
private:
std::auto_ptr<int> _iterator;
// int *_iterator;
};

class collection {
public:
iterator begin() {
return iterator();
}
};


int main() {
collection va;
iterator it(va.begin());
}
/***********************************/


The error message:

main_test.cc: In member function `iterator collection::begin()':
main_test.cc:49: error: no matching function for call to
`iterator::iterator(iterator)'
main_test.cc:40: note: candidates are: iterator::iterator(iterator&)
main_test.cc: In function `int main()':
main_test.cc:56: error: no matching function for call to
`iterator::iterator(iterator)'
main_test.cc:49: note: candidates are: iterator::iterator()
main_test.cc:40: note: iterator::iterator(iterator&)
 
A

Alf P. Steinbach

* (e-mail address removed):
I have the following program. The auto_ptr cause the compile error.
When I change auto_ptr to pointer, the error gone.

Would you please help me to understand what is wrong?

/*******************************************/
class iterator {
private:
std::auto_ptr<int> _iterator;
// int *_iterator;
};

class collection {
public:
iterator begin() {
return iterator();

'iterator()' is a temporary. You can't bind a temporary to a reference
to non-const. But the only copy constructor you have available has
argument type 'iterator&', a reference to non-const (because that's the
only one that can be generated that can handle the auto_ptr member), and
so the compiler complains.

You could do this like

iterator begin()
{
iterator result;
return result;
}

but having that auto_ptr in there is most probably a design error,
because it seems to be 100% meaningless, and you would need to add
additional support to make the function useful to client code.

Do you really want an iterator refer to and own a dynamically allocated int?
 
P

PengYu.UT

Alf said:
* (e-mail address removed):

'iterator()' is a temporary. You can't bind a temporary to a reference
to non-const. But the only copy constructor you have available has
argument type 'iterator&', a reference to non-const (because that's the
only one that can be generated that can handle the auto_ptr member), and
so the compiler complains.

You could do this like

iterator begin()
{
iterator result;
return result;
}

The compilor reports error, too. Is there anything else wrong?
but having that auto_ptr in there is most probably a design error,
because it seems to be 100% meaningless, and you would need to add
additional support to make the function useful to client code.

This is just a mini example reduced from a rather long segment of code.
Do take it too serious?
 
M

michaelkatsilis

Well, the design doesn't really make sense for a start. Why would you
have an iterator class with one member and no constructors or other
methods, and that points to int types? Maybe read up on auto_ptr, which
will give you an idea of its purpose in relation to raw pointers. A
default constructor, copy constructor and maybe even a destructor
wouldn't go astray, at least the code will compile.

If you want to write a container and iterator class, take a look at the
design and implementation of the STL which will help a great deal.
The error message:

main_test.cc: In member function `iterator collection::begin()':
main_test.cc:49: error: no matching function for call to
`iterator::iterator(iterator)'

This refers to the constr...
main_test.cc:40: note: candidates are: iterator::iterator(iterator&)

This is what you have.
main_test.cc: In function `int main()':
main_test.cc:56: error: no matching function for call to
`iterator::iterator(iterator)'

You don't have this.
main_test.cc:49: note: candidates are: iterator::iterator()

You do have this.
main_test.cc:40: note: iterator::iterator(iterator&)

Regards,

Michael
 
M

michaelkatsilis

Apart from what Alf has already pointed out regarding the temporary
returned by collection::begin(), a few comments. The design doesn't
really make sense for a start. Why would you have an iterator class
with one member and no constructors or other methods, and that points
to int types? Maybe read up on auto_ptr, which will give you an idea of
its purpose in relation to raw pointers. A default constructor, copy
constructor and maybe even a destructor wouldn't go astray, at least
the code will compile.

If you want to write a container and iterator class, take a look at the
design and implementation of the STL which will help a great deal.
The error message:

main_test.cc: In member function `iterator collection::begin()':
main_test.cc:49: error: no matching function for call to
`iterator::iterator(iterator)'

This refers to the constr...
main_test.cc:40: note: candidates are: iterator::iterator(iterator&)

This is what you have.
main_test.cc: In function `int main()':
main_test.cc:56: error: no matching function for call to
`iterator::iterator(iterator)'

You don't have this.
main_test.cc:49: note: candidates are: iterator::iterator()

You do have this.
main_test.cc:40: note: iterator::iterator(iterator&)

Regards,

Michael
 

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
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top