2

I am trying to insert a reference to my object, but i am getting large number of errors. What do i need to modify in the custom object, so that it can be inserted successfully?

The code is shown below:

#include <map>
#include <iostream>
#include <string>

using namespace std;

class A
{
public:
    A()
    {
            cout << "default constructor" << endl;
    }

    A(A & a)
    {
            cout << "copy constructor" << endl;
    }

    A & operator=(A & a)
    {
            cout << "assignment operator" << endl;
            return *this;
    }

    ~A()
    {
            cout << "destructor" << endl;
    }
};

int main()
{
    map<string, A&> m1;
    A a;
    m1["a"] = a;
    return 0;
}

UPDATE:

  1. It is possible to create a map with reference such as map<string, A&>

  2. The error was in usage of [] operator. By making following change, the code works

    typedef map<string, A&> mymap;
    
    int main()
    {
       mymap m1;
       A a;
       cout << &a << endl;
       m1.insert(make_pair<string, A&>("a", a));
       mymap::iterator it = m1.find("a");
       A &b = (*it).second;
       cout << &b << endl; // same memory address as a
       return 0;
    }
    
2
  • cppreference.com is very useful website. Commented Mar 20, 2013 at 18:49
  • 2
    Note that your copy constructor A(A&a) is not the one you want -- you want A(A const&), and the same for operator=. This is unrelated to your problem, which is solved below. Commented Mar 20, 2013 at 18:49

3 Answers 3

4

You can't store references in map. Use pointers instead.

Replace:

map<string, A&> m1;

With:

map<string, A*> m1;

Or better yet (Thanks WhozCraig!):

map<string, shared_ptr<A> > m1;
Sign up to request clarification or add additional context in comments.

1 Comment

+1 And use pointers with brains (i.e. smart pointers; not naked pointers). Though I would prefer to just store the actual objects in the map
2

You can not use references as the key- or value-type of an container. You need to use a pointer, preferably a smart pointer like std::shared_ptr or, if this is not too expensive, you could store copies of the objects. Here's some options:

map<string,A> mc; // stores copies of A

map<string,A*> mp; // you need to take care of memory management - avoid that

map<string,shared_ptr<A>> msp; // prefered

to use the latter, you could create and insert elements like this:

msp["a"] = make_shared<A>();

Hope it helps as a start.

2 Comments

The copy ctor and assignment operator will have to be changed to use map<string,A> it does not compile as is
@ShafikYaghmour: True, but I see it as more-or-less unrelated as it should be fixed anyways, not just if OP is trying to store it in a map. He will automatically stumble upon that problem when he tries the first approach and either he knows how to fix it or he knows how to ask a question on SO.
0

You're attempting to have a map that only stores references. This is a bit impossible because references usually have to be initialized with a reference the moment they are brought into conception

When you use m1["a"], that function by itself has to default-construct and item in-place before letting you assign to it (you can't default-construct a reference). If you're trying to avoid having a copy made, you can use the emplace flavor of functions to have the object constructed in-place, or just have map<std::string, A> and make your life easy.

If you need it to not make a copy, you can also try using a map<std::string, A*> that stores pointers. You'll be responsible for cleaning up the memory yourself, however (unless you use std::unique_ptr or a friend of that).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.