Problem solving-use a custom class in the STL queue

Source: Internet
Author: User

This article is original and reprinted. Please ensure the integrity of the article and indicate the source explicitly.

Link: http://blog.csdn.net/wlsgzl/article/details/38843513

STL is rarely used at ordinary times. Even if it is used, it basically uses simple data types such as queue <int>, and occasionally writes small code in MFC, with queue <cstring>.

(Please do not tell me why I use cstring instead of string. It is really convenient to use cstring in MFC. I have no requirements on memory utilization and processing speed. Just run it, do not vomit .)

= ====================

When I used a custom class this time, something went wrong.

As a little monkey who works at the bottom layer, he is conscientious and hard-working. When writing class code, no matter whether it is useful or useless. He is used to adding "copy constructor" and "overload = ".

So I declared

std::queue<CDataRecv> m_queDataRecv;
After compilation, many items are output. (In order not to disclose information, I replaced the relevant path with XXXXXXXXX)

C: \ Program Files \ Microsoft Visual Studio 10.0 \ Vc \ include \ xmemory (48): Error c2558: Class "cdatarecv ": no copy constructor is available or the copy constructor is declared as "Explicit" 1> C: \ Program Files \ Microsoft Visual Studio 10.0 \ Vc \ include \ xmemory (197 ): see instantiate "Void STD: _ construct <cdatarecv, const _ ty &> (_ ty1 *, _ ty2) for the function template being compiled) "Reference 1> With1> [1> _ ty = cdatarecv, 1> _ ty1 = cdatarecv, 1> _ ty2 = const cdatarecv & 1>] 1> C: \ Program Files \ Microsoft Visual Studio 10.0 \ Vc \ include \ xmemory (196): Compile the class template member function "Void STD: Allocator <_ ty >:: construct (cdatarecv *, const _ ty &) "1> With1> [1> _ ty = cdatarecv1>] 1> C: \ Program Files \ Microsoft Visual Studio 10.0 \ Vc \ include \ deque (721): For details, see instantiate the "STD :: reference 1> With1> [1> _ ty = cdatarecv1>] 1> C: \ Program Files \ Microsoft Visual Studio 10.0 \ Vc \ include \ deque (773): For details, refer to instantiating the "STD: _ deque_val <_ ty, _ alloc> "Reference 1> With1> [1> _ ty = cdatarecv, 1> _ alloc = STD: Allocator <cdatarecv> 1>] 1> C: \ Program Files \ Microsoft Visual Studio 10.0 \ Vc \ include \ queue (22): see instantiate the "STD :: deque <_ ty> reference 1> With1> [1> _ ty = cdatarecv1>] 1> D: \ xxxxxxxxxxxxxxxxxxxxxxxxx \ mscommwls. H (126): see reference 1> With1> [1> _ ty = cdatarecv1> to instantiate a compiled class template
There is a key issue here. Generally, when I see the following content, I will naturally check my own constructor to see if I have written less. But this time I'm sure I did.
No available copy constructor or copy constructor declared as "Explicit"

So let's look down.

Reference 1> With1> [1> _ ty = cdatarecv, 1> _ ty1 = cdatarecv, 1> _ ty2 = const cdatarecv & 1>]
This is very important. I have not noticed it:
const _Ty&<pre name="code" class="cpp">_Ty2=const CDataRecv &

STL uses const for the purpose of "You understand", and the copy const of my custom class does not add Const. The problem is found! 

So I quickly added the const modifier to the copy constructor, and the problem was solved.

Now, sometimes it is not just to prevent modification. To use other existing tools, it should comply with the requirements/specifications of the tool.

------------------------------------------- Split line? No, I'm a single dog. -------------------------------------------

Because STL is rarely used, I am very happy to have this problem, not just standardizing code and code specifications.


Problem solving-use a custom class in the STL queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.