Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 28th, 2006, 03:25 AM
PengYu.UT@gmail.com
Guest
 
Posts: n/a
Default About default copy constructor when using auto_ptr as the member

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&)

  #2  
Old March 28th, 2006, 03:35 AM
Alf P. Steinbach
Guest
 
Posts: n/a
Default Re: About default copy constructor when using auto_ptr as the member

* PengYu.UT@gmail.com:[color=blue]
> 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();[/color]

'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?
[color=blue]
> }
> };[/color]


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
  #3  
Old March 28th, 2006, 04:35 AM
PengYu.UT@gmail.com
Guest
 
Posts: n/a
Default Re: About default copy constructor when using auto_ptr as the member


Alf P. Steinbach wrote:[color=blue]
> * PengYu.UT@gmail.com:[color=green]
> > 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();[/color]
>
> '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;
> }[/color]

The compilor reports error, too. Is there anything else wrong?
[color=blue]
>
> 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.[/color]

This is just a mini example reduced from a rather long segment of code.
Do take it too serious?[color=blue]
>
> Do you really want an iterator refer to and own a dynamically allocated int?
>[color=green]
> > }
> > };[/color]
>
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?[/color]

  #4  
Old March 28th, 2006, 05:25 AM
michaelkatsilis@yahoo.com
Guest
 
Posts: n/a
Default Re: About default copy constructor when using auto_ptr as the member

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.
[color=blue]
> 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)'[/color]

This refers to the constr...
[color=blue]
> main_test.cc:40: note: candidates are: iterator::iterator(iterator&)[/color]

This is what you have.
[color=blue]
> main_test.cc: In function `int main()':
> main_test.cc:56: error: no matching function for call to
> `iterator::iterator(iterator)'[/color]

You don't have this.
[color=blue]
> main_test.cc:49: note: candidates are: iterator::iterator()[/color]

You do have this.
[color=blue]
> main_test.cc:40: note: iterator::iterator(iterator&)[/color]

Regards,

Michael

  #5  
Old March 28th, 2006, 05:35 AM
michaelkatsilis@yahoo.com
Guest
 
Posts: n/a
Default Re: About default copy constructor when using auto_ptr as the member

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.
[color=blue]
> 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)'[/color]

This refers to the constr...
[color=blue]
> main_test.cc:40: note: candidates are: iterator::iterator(iterator&)[/color]

This is what you have.
[color=blue]
> main_test.cc: In function `int main()':
> main_test.cc:56: error: no matching function for call to
> `iterator::iterator(iterator)'[/color]

You don't have this.
[color=blue]
> main_test.cc:49: note: candidates are: iterator::iterator()[/color]

You do have this.
[color=blue]
> main_test.cc:40: note: iterator::iterator(iterator&)[/color]

Regards,

Michael

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles