Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7754001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:11:49+00:00 2026-06-01T12:11:49+00:00

I have a class with a std::map used as a container. I want to

  • 0

I have a class with a std::map used as a container. I want to add a function to copy the map between two objects. Since the map was declared as a private member of the class, I need a friend function to do so. Here is my code:

class Data;
void copyData(Data &, Data &);

class Data
{
  private:
    map<int, int> _data;

  public:
    friend void copyData(Data &, Data&);
};

void copyData(Data &a, Data &b)
{
  std::copy(a._data.begin(), a._data.end(), b._data.begin());
}

main()
{
   // initialization here
   Data A, B;
   copyData(A, B);
}

But there are many warnings while compiling with mingw32. How do I do this correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:11:51+00:00Added an answer on June 1, 2026 at 12:11 pm

    The std::map<K,V>::value_type is defined as std::pair<const K,V>. This ensures that std::map<K,V>::iterator objects can’t be used to assign to the key (or else you could use this to break the container invariants).

    This means that map iterators don’t satisfy the OutputIterator concept requirements and you can’t use them as the third parameter of the std::copy() function. On the same note, there are lots of situations that would make this code break anyways, such as if one map has more associations than the other.

    Besides, there is a much easier way to write your function:

    void copyData(Data &a, Data &b)
    {
      b._data = a._data;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a templatized container class in C++ which is similar to a std::map
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a class containing a std::unique_ptr<> and I want to put instances of
I have a class with a static std::map member variable that maps char s
Assume I have a class foo, and wish to use a std::map to store
So I have such function: boost::shared_ptr<my_class> get_class_by_name(std::string name) { typedef std::map<boost::shared_ptr<my_class>, my_description> map_t; BOOST_FOREACH(map_t::value_type
I have a class with a map, and I want to serialize the class
I have some opaque bytes which I want to use in an std::map ,
When I have a template class which contains template map and a const_iterator declared
I have one base class which holds a map for function pointers like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.