在 stl Multimap 中将自己的对象作为数据

发布于 2024-08-03 16:26:18 字数 300 浏览 4 评论 0原文

我正在编写一个应用程序,我想将字符串存储为键,将自定义对象存储为值

multimap<string, owncreatedobject> mymap;

编译效果很好,但在使用函数插入时出现“分段错误” 在运行时。

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

已经添加了一个复制构造函数和一个赋值函数(它调用复制构造函数),您

对“分段错误有什么想法吗?

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 function 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

脸赞 2024-08-10 16:26:18

查看您为复制构造函数和赋值运算符实现添加的注释,我发现您的赋值运算符实现是错误的。基本上它不会复制任何内容,而是使用语句 Filter(f); ,您正在创建一个名为 f 的本地对象。你不能像那样调用复制构造函数。我建议您编写一个私有复制方法并在复制构造函数和赋值运算符中使用它。

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.

白日梦 2024-08-10 16:26:18

我花了一个小时摆弄你的问题,我想我明白了。

尝试在此代码段中使用某些内容:

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

在给定的示例中,您尝试在没有迭代器的情况下插入。多重映射的 2 元素插入调用的第一个元素是迭代器。这就是分段错误的原因以及应该使用pair的原因。

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.

瀟灑尐姊 2024-08-10 16:26:18

我怀疑您的复制构造函数和/或赋值运算符实现有错误。

如果线路

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

崩溃,很可能是在“myobject”上执行的某些方法导致了问题。在此代码行中,最有趣的函数是复制构造函数和/或赋值运算符。

您说您已经添加了一个赋值运算符(它调用复制构造函数),但这听起来很奇怪。通常情况恰恰相反。复制构造函数根据需要分配资源(即内存),然后调用赋值运算符。

更新:阅读您的评论后,您的复制构造函数应如下所示:

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

这是赋值运算符。请注意它如何复制这些值。您可能根本不需要赋值运算符(或复制构造函数)实现,因为编译器合成的代码适合您的使用。

Filter &Filter::operator=( const Filter &rhs ) {
    SrcNET = f.SrcNET;
    SrcPort = f.SrcPort;
    DstNET = f.DstNET;
    FlowLabel = f.FlowLabel;
    return *this;
}

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;
}
沫离伤花 2024-08-10 16:26:18

我假设您正在尝试调用接受多重映射的 value_typeinsert 版本。在这种情况下,只需记住 multimap 的 value_type 是一个 std::pair。然后,尝试以下操作:


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

编辑(在您的评论之后):您的赋值运算符包含错误。您创建了一个没有任何用途的临时对象。下面是使用交换惯用法的实现建议。 (请注意,我不知道类成员的类型是什么。假设它们是内置函数,例如 int。)


void Swap(Filter const& f) //Create a swap member function.
{
  std::swap(SrcNET, f.SrcNET);
  std::swap(SrcPort, f.SrcPort);
  std::swap(DstNET, f.DstNET); 
  std::swap(DstPort, f.DstPort); 
  std::swap(FlowLabel, f.FlowLabel);   
}

Filter& Filter::operator =(Filter f)
{ 
  swap(f); //Swaps the copy created for parameter passing (pass-by-value) above.
  return *this;
} 

I assume you're trying to call the insert version that accepts a value_type of the multimap. In this case, just remember that multimap's value_type is a std::pair. Then, try this:


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

EDIT (after your comment): Your assignment operator contains an error. You create a temporary that serves for no purpose. Below is a suggestion of implementation using the swap idiom. (Notice that I don't know what are the types of the class member. Suppose they're a builtin such as int for example.)


void Swap(Filter const& f) //Create a swap member function.
{
  std::swap(SrcNET, f.SrcNET);
  std::swap(SrcPort, f.SrcPort);
  std::swap(DstNET, f.DstNET); 
  std::swap(DstPort, f.DstPort); 
  std::swap(FlowLabel, f.FlowLabel);   
}

Filter& Filter::operator =(Filter f)
{ 
  swap(f); //Swaps the copy created for parameter passing (pass-by-value) above.
  return *this;
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文