473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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

Mar 28 '06 #1
4 3101
* Pe*******@gmail.com:
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?
}
};

--
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?
Mar 28 '06 #2

Alf P. Steinbach wrote:
* Pe*******@gmail.com:
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;
}


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?
Do you really want an iterator refer to and own a dynamically allocated int?
}
};

--
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?


Mar 28 '06 #3
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

Mar 28 '06 #4
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

Mar 28 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
10
by: dragoncoder | last post by:
Hi all, I am trying to understanding std::auto_ptr<Tclass implementation from "The C++ standard library" by Nicolai Josuttis. He gives a sample implementation of auto_ptr class template in...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
2
by: timlyee | last post by:
int *p = new int; auto_ptr<intap1 = p; //will fail on 3 1 auto_ptr<intap1(p); //ok 2 *ap1 = 12; // 3 the first situation has called :...
7
by: Scott Gifford | last post by:
As a possible solution to a problem I'm trying to solve with an iterator (see an earlier post by me with subject "Iterator implementation questions: copy constructor and postfix increment"), I'm...
10
by: JosephLee | last post by:
In Inside C++ object Model, Lippman said there are four cases in which compile will sythesize a default constructor to initialize the member variables if the constructor is absent: 1. there is a...
2
by: dolphin | last post by:
Hi All Today , I read the source code of auto_ptr. I have a question about the copy constructor of auto_ptr. template<class _Ty> class auto_ptr { .................. } there is a copy...
9
by: George2 | last post by:
Hello everyone, I am wondering the default implementation of assignment operator (e.g. when we do not implement assignment operator in user defined class, what will be returned? temporary...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.