tags:

views:

262

answers:

4

I'm writing an application, where I want to store strings as keys ans a custom Object as value

multimap<string, owncreatedobject> mymap;

Compiling does well, but I get a "Segmentation fault" when using the funtion insert during runtime.

mymap.insert(string,myobject); --> Segmentation Error

A already added a copyconstructor an assignmentfunction (which calls the copyconstructor)

any idea about the "Segmentation fault?

A: 
ltcmelo
I tried, but I get the Segmentation Fault stillPerhaps I have to write a Allocator for my object?
augi
Then, please post a small but relevant portion of the code that reproduces the problem. Could be an error at the copy constructor, program logic in general, or something else. I don't think an allocator is the cause of the error.
ltcmelo
A: 

I suspect that you have a buggy copy constructor and/or assignment operator implementation.

If the line

mymap.insert(std::make_pair(mystring,myobject));

crashes, it's pretty likely that some method which is executed on 'myobject' causes the problem. In this code line, the most interesting functions are the copy constructor and/or the assignment operator.

You say that you already added an assignment operator (which calls the copy constructor), but that sounds strange. Usually it's exactly the other way round; the copy constructor allocates resources (i.e. memory) as needed and then calls the assignment operator.

UPDATE: After reading your comment, here's how your copy constructor should look like:

Filter::Filter( const Filter &rhs ) {
    // Apparently no resource acquisition like memory allocation is necessary,
    // so just assign the rhs value.
    *this = rhs;
}

And here's the assignment operator. Note how it just copies over the values. Chances are that you don't need an assignment operator (or copy constructor) implementation at all because the compiler-synthesized code is fine for your uses.

Filter &Filter::operator=( const Filter &rhs ) {
    SrcNET = f.SrcNET;
    SrcPort = f.SrcPort;
    DstNET = f.DstNET;
    FlowLabel = f.FlowLabel;
    return *this;
}
Frerich Raabe
header: Filter Filter(const Filter cpp:Filter } return *this;}Filter::Filter(const Filter SrcPort = f.SrcPort; DstNET = f.DstNET; DstPort = f.DstPort; FlowLabel = f.FlowLabel;}
augi
Your assignment operator is bogus. It doesn't affect 'this' at all. Check my updated response for how these two functions should be implemented.
Frerich Raabe
+1  A: 

Looking at the comment you added for the copy constructor and assignment operator implementation, I found that your assignment operator implementation is wrong. Basically it is not copying anything, instead with the statement Filter(f); , you are creating a local object named f. You can not call copy constructor like that. I suggest you to write a private copy method and use it in both copy ctor and assignment operator.

Naveen
A: 

I fiddle with your problem for the last hour and I think I got it.

Try using something in the matter of this code segment:

multimap<string,yourclass const*> mymap
void addtomap(string s,yourclass const* c)
{
   mymap.insert(pair<string, yourclass const*>(s,c));
}
...`addtomap("abc",&z);

In your given example you tried to insert without an iterator. The first element for a 2 element insert call for a multimap is the iterator. That's the reason for your segmentation fault and why one should use pair.

Stefan Sauer