Map of objects

A

Andrea Crotti

I have the impression that I asked that already but I can't find any
answer so I try again...

Suppose

class Obj {
Obj::Obj(int x);
};

and I want somewhere else

std::map<Obj, std::vector<Obj> > mymap;

But how do I declare it since here I don't know how to initialize this...?
 
V

Victor Bazarov

I have the impression that I asked that already but I can't find any
answer so I try again...

Suppose

class Obj {
Obj::Obj(int x);

No default constructor?
};

and I want somewhere else

std::map<Obj, std::vector<Obj> > mymap;

But how do I declare it since here I don't know how to initialize this...?

First, your type has to be less-comparable. Second, initialize what?
Please write at least some *pseudo-code* that has some behavior so we
could see what you are trying to *accomplish*.

V
 
A

Andrea Crotti

Victor Bazarov said:
No default constructor?

Well I could add the default constructor yes, but then I have to set it
right after, not very nice.
That probably would solve my problem but I still want to know how I
could solve it in another way...
First, your type has to be less-comparable. Second, initialize what?
Please write at least some *pseudo-code* that has some behavior so we
could see what you are trying to *accomplish*.

V

Ah good I didn't know, I've added it, but still I get this error

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_pair.h:
In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&)
[with _U1 = PadNodeID, _U2 = PadNodeID, _T1 = const PadNodeID, _T2 =
RingBuffer<PadNodeID>]’

What I'm trying to do is to simply insert something in the map

--8<---------------cut here---------------start------------->8---
if (trace.count(index) == 0) {
trace.insert(make_pair(index, node));
}
--8<---------------cut here---------------end--------------->8---

Is this correct for the comparison operator?
bool PadNodeID::eek:perator<(PadNodeID other) const
 
A

Andrea Crotti

Andrea Crotti said:
Ah good I didn't know, I've added it, but still I get this error

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_pair.h:
In constructor ‘std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&)
[with _U1 = PadNodeID, _U2 = PadNodeID, _T1 = const PadNodeID, _T2 =
RingBuffer<PadNodeID>]’

What I'm trying to do is to simply insert something in the map

if (trace.count(index) == 0) {
trace.insert(make_pair(index, node));
}

Is this correct for the comparison operator?
bool PadNodeID::eek:perator<(PadNodeID other) const

Ok well it was wrong what I wrote before
Now the first part works perfectly

if (trace.count(index) == 0) {
RingBuffer<PadNodeID> empty(GLOBALS::trace_length);
trace.insert(make_pair(index, empty));
}

But then I want to actually add the element to the ringbuffer with
RingBuffer<PadNodeID> tmp = trace[index];
tmp.push(node);

But it complains again with

--8<---------------cut here---------------start------------->8---
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_map.h:451:11: error: no matching function for call to ‘RingBuffer<PadNodeID>::RingBuffer()’
RingBuffer.h:16:5: note: candidates are: RingBuffer<T>::RingBuffer(size_t) [with T = PadNodeID, size_t = long unsigned int]
RingBuffer.h:8:1: note: RingBuffer<PadNodeID>::RingBuffer(const RingBuffer<PadNodeID>&)
--8<---------------cut here---------------end--------------->8---

But why I don't get it, why should it want again the size constructor
when I'm taking something that should be ALREADY a created object?
 
S

SG

Ok well it was wrong what I wrote before
Now the first part works perfectly

    if (trace.count(index) == 0) {
        RingBuffer<PadNodeID> empty(GLOBALS::trace_length);
        trace.insert(make_pair(index, empty));
    }

What is trace? What is RingBuffer? I'm /assuming/ that trace refers to
an object of type std::map said:
But then I want to actually add the element to the ringbuffer with
    RingBuffer<PadNodeID> tmp = trace[index];
    tmp.push(node);

Two issues: 1st, if you want to use operator[] on a map, the
mapped_type -- in your case RingBuffer<PadNodeID> -- has to be default
constructible. This is because the operator might have to default
construct such an object in case there is no such key/value pair yet.
2nd, this line is a copy-initialization. The RingBuffer object would
be copied so that tmp refers to a copy. Any modifications of the
object tmp refers to won't change the object that is stored on the
map. It seems you're not aware of the differences between C++ and,
say, Java in this respect. Better grab a decent C++ book and learn the
language properly.

If you're sure that such a RingBuffer objects already exists, you can
use find. Also, if you want to keep a reference to the RingBuffer, you
have to say so:

RingBuffer said:
But why I don't get it, why should it want again the size constructor
when I'm taking something that should be ALREADY a created object?

Doesn't matter. The operator[] implementation works like this:

iterator it = this->find(key);
if (it==this->end()) {
return this->insert(value_type(key,mapped_type())->second;
} else { // ^^^^^^^^^^^^^
return it->second;
}

Even if the object exists in /some situation/ at /runtime/, we still
need the compiler to translate the first branch as well for the cases
where the object doesn't exist. And the first branch requires
mapped_type to be default constructible. That's simply a precondition
for this operator. If this precondition is not satisfied, you can't
use the operator -- regardless of whether the object already exists or
not.

Cheers!
SG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,200
Latest member
LaraHunley

Latest Threads

Top